双向的多对多关联,要求关联的双方实体类都使用Set集合属性,两端都增加集合属性的setter和getter方法。
Student类
package Mapping3;
import java.util.*;
public class Student {
private Long id;
private long studentNo;
private String studentName;
private int sage;
private String major;
/* 进行多对多关联,双方互添引用对方对象的属性及方法 */
private Set<Course> courses = new HashSet<Course>();
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
public Student() {
// TODO Auto-generated constructor stub
}
public Student(long studentNo, String studentName, int sage, String major) {
this.studentNo = studentNo;
this.studentName = studentName;
this.sage = sage;
this.major = major;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public long getStudentNo() {
return studentNo;
}
public void setStudentNo(long studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- "http://hibernate.sourceforge.net/dtd/hibernate-mapping-3.0.dtd"> -->
<hibernate-mapping package="Mapping3">
<class name="Student" table="student" >
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="studentNo" type="long" column="student_no"/>
<property name="studentName" type="string" column="student_name"/>
<property name="sage" type="integer" column="sage"/>
<property name="major" type="string" column="major"/>
<!-- 双向多对多关联 -->
<set name="courses" table="student_course" cascade="all" >
<key column="student_id" />
<many-to-many column="course_id" class="Course" />
</set>
</class>
</hibernate-mapping>
Course类
package Mapping3;
import java.util.*;
public class Course {
private Long id;
private String courseName;
private double ccredit;
private Set<Student> students = new HashSet<Student>();
public Course() {
// TODO Auto-generated constructor stub
}
public Course(String courseName, double ccredit) {
super();
this.courseName = courseName;
this.ccredit = ccredit;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public double getCcredit() {
return ccredit;
}
public void setCcredit(double ccredit) {
this.ccredit = ccredit;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Course.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="Mapping3">
<class name="Course" table="course" >
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="courseName" type="string" column="course_name"/>
<property name="ccredit" type="double" column="ccredit"/>
<!-- 双向多对多关联 -->
<set name="students" table="student_course" cascade="all" >
<key column="course_id" />
<many-to-many column="student_id" class="Student" />
</set>
</class>
</hibernate-mapping>
测试
package Mapping3;
import java.util.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;
public class ManyToManyBidirectionAssociation {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Student student1 = new Student(20140101,"王小明",18,"计算机科学"),
student2 = new Student(20140102,"李大海",20,"电子商务");
Course course1 = new Course("数据结构",4),
course2 = new Course("操作系统",3),
course3 = new Course("数据库原理",3.5);
Set<Course> courses1 = new HashSet<Course>();
courses1.add(course1);
courses1.add(course2);
student1.setCourses(courses1); //student1选2门课
Set<Course> courses2 = new HashSet<Course>();
courses2.add(course1);
courses2.add(course2);
courses2.add(course3);
student2.setCourses(courses2); //student2选3门课
session.save(student1);
session.save(student2);
tx.commit();
}
}
结果