多对多: 学生与课程,以下主要从案例出发,由案例具体解析
单向多对多
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hbsi.many2many">
<class name="Student" table="student">
<id name="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<set name="course" table="student_course" cascade="save-update">
<key column="student_id"/>
<many-to-many column="course_id" class="Course" />
</set>
</class>
</hibernate-mapping>
Course.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hbsi.many2many">
<class name="Course" table="course">
<id name="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
测试类
package com.hbsi.many2many;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.hbsi.utils.HibernateSessionFactory;
public class TestApp {
Session session = HibernateSessionFactory.getSession();
@Test
public void add(){
session.beginTransaction();
Student student1 = new Student();
student1.setName("keven");
Student student2 = new Student();
student2.setName("jie");
Course course1 = new Course();
course1.setName("english");
Course course2 = new Course();
course2.setName("chinese");
Set<Course> courses = new HashSet<Course>();
courses.add(course1);
courses.add(course2);
student1.setCourse(courses);
session.save(student1);
session.save(student2);
session.save(course1);
session.save(course2);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
@Test
public void find(){
Student student = (Student) session.get(Student.class,1);
Set<Course> courses = student.getCourse();
for(Course course : courses){
System.out.println(student.getName()+"---"+course.getName());
}
}
}
双向多对多
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hbsi.many2manyboth">
<class name="Student" table="studentboth">
<id name="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<set name="course" table="student_courseboth" cascade="save-update">
<key column="student_id"/>
<many-to-many column="course_id" class="Course" />
</set>
</class>
</hibernate-mapping>
Course.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hbsi.many2manyboth">
<class name="Course" table="courseboth">
<id name="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<set name="student" table="student_courseboth" cascade="save-update">
<key column="course_id"/>
<many-to-many class="Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
测试类
package com.hbsi.many2manyboth;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.junit.Test;
import com.hbsi.utils.HibernateSessionFactory;
public class TestApp {
Session session = HibernateSessionFactory.getSession();
@Test
public void add(){
session.beginTransaction();
Student student1 = new Student();
student1.setName("keven");
Student student2 = new Student();
student2.setName("jie");
Course course1 = new Course();
course1.setName("english");
Course course2 = new Course();
course2.setName("chinese");
/*达到的添加功能一样,可以从多方的任何一方添加
* Set<Course> courses = new HashSet<Course>();
courses.add(course1);
courses.add(course2);
student1.setCourse(courses);
student2.setCourse(courses);*/
Set<Student> students = new HashSet<Student>();
students.add(student1);
students.add(student2);
course1.setStudent(students);
course2.setStudent(students);
session.save(student1);
session.save(student2);
session.save(course1);
session.save(course2);
session.getTransaction().commit();
HibernateSessionFactory.closeSession();
}
@Test
public void find(){
Student student = (Student) session.get(Student.class,1);
Set<Course> courses = student.getCourse();
for(Course course : courses){
System.out.println(student.getName()+"---"+course.getName());
}
Course course1 = (Course) session.get(Course.class,1);
Set<Student> students = course1.getStudent();
for(Student student1 : students){
System.out.println(course1.getName()+"---"+student1.getName());
}
}
}