Hibernate關係對映之多對多對映的兩種方式
阿新 • • 發佈:2019-02-15
多對多關係##
many-to-many
應用場景:不建議使用,建議拆解成兩個“一對多”連線
關係定義(主控方):
//xml對映方法
<set name="propertyName" table="middle_table_name">
<key column="columnName_master" />
<many-to-many class="className" column="columnName_slave" />
</set>
//annotation註解對映方法
@ManyToMany
@JoinTable(name="middle_table_name"
joinColumns=@JoinColumn(name="columnName_master",
inverseJoinColumns=@JoinColumn(name="columnName_slave"))
)
關係定義(被控方)
//xml對映
<set name="propertyName" table="middle_table_name">
<key column="columnName_slave" />
<many-to-many class="className" column="columnName_master" />
</key>
//annotation註解對映
@ManyToMany(mappedBy="propertyName")
多對多對映程式碼##
說明:學生與學生之間的關係是多對多,一個學生可以對應多個教師,一個教師也可以對應多個學生。這裡定義一個學生類Student和一個教師類Lecturer。
一.xml對映
1.新建Student.hbm.xml檔案,在該檔案裡編寫對映關係,Student為主控方。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ffy.studentmanage.Student" table="student">
<id name="id" column="id">
<generator class="assigned"></generator>
</id>
<property name="sid" column="sid"></property>
<property name="firstName" column="firstname"></property>
<property name="lastName" column="lastname"></property>
<property name="gender" column="gender"></property>
<property name="phone" column="phone"></property>
<property name="email" column="email"></property>
<property name="dateOfBirth" column="date_of_birth"></property>
//一個學生可以有多個老師,所以需要用集合來放教師類。
//name:在student類中有一個存放型別為Lecturer的集合,這裡的name為該set。table:學生和教師關係的中間表名稱
<set name="lecturers" table="student_has_teacher">
//column:中間表的用於與student(我方)關聯的外來鍵。
<key column="student_id"></key>
//column:中間表的用於與lecturer(對方)關聯的外來鍵。 class:表示對方的類
<many-to-many column="lecturer_id" class="com.ffy.studentmanage.Lecture"></many-to-many>
</set>
</class>
</hibernate-mapping>
2.新建Leturer.hbm.xml(對方),填寫對映關係
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ffy.studentmanage.Lecture" table="lecturer">
<id name="id" column="id">
<generator class="assigned"></generator>
</id>
<property name="uid" column="uid"></property>
<property name="firstName" column="firstname"></property>
<property name="lastName" column="lastname"></property>
<property name="phone" column="phone"></property>
<property name="collegeId" column="college_id"></property>
//與上一步類似
<set name="students" table="student_has_teacher">
<key column="lecturer_id"></key>
<many-to-many column="student_id" class="com.ffy.studentmanage.Student"></many-to-many>
</set>
</class>
</hibernate-mapping>
3.在hibernate.cfg.xml中註冊上面兩個xml檔案。
<mapping resource="Student.hbm.xml"/>
<mapping resource="Lecturer.hbm.xml"/>
二.annotation註解對映
1.在hibernate.cfg.xml中註冊Student.class類和Lecturer.class類。
<mapping class="com.ffy.studentmanage.Student"/>
<mapping class="com.ffy.studentmanage.Lecture"/>
2.在Student.class類裡面添加註解。
package com.ffy.studentmanage;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
@Id
@Column(name="id")
private int id;
@Column(name="sid")
private int sid;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
@Column(name="gender")
private String gender;
@Column(name="phone")
private String phone;
@Column(name="email")
private String email;
@Column(name="date_of_birth")
private String dateOfBirth;
@Column(name="height")
private double height;
//宣告多對多關係
@ManyToMany
@JoinTable(name="student_has_teacher",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="lecturer_id"))
private Set<Lecture> lecturers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastname() {
return lastName;
}
public void setLastname(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<Lecture> getLecturers() {
return lecturers;
}
public void setLecturers(Set<Lecture> lecturers) {
this.lecturers = lecturers;
}
// public String toString(){
// return this.id+":"+this.lastName+this.firstName+this.gender+":"+this.phone+":"+this.email+";username"+studentLogin.getUsername()+";password:"+studentLogin.getPassword();
// }
}
3.在Lecturer.class裡面添加註解
package com.ffy.studentmanage;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="lecturer")
public class Lecture {
@Id
@Column(name="id")
private int id;
@Column(name="uid")
private String uid;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
@Column(name="phone")
private String phone;
@Column(name="college_id")
private int collegeId;
//將許可權交由對方,即Student.class。主控方與被控方通過主控方里的lecturers屬性建立聯絡。
@ManyToMany(mappedBy="lecturers")
private Set<Student> students;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getCollegeId() {
return collegeId;
}
public void setCollegeId(int collegeId) {
this.collegeId = collegeId;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}