Hibernate多对多关系

Posted JustDo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate多对多关系相关的知识,希望对你有一定的参考价值。

 版权声明:本文为博主原创文章,如需转载请标注转载地址。

 博客地址:http://www.cnblogs.com/caoyc/p/5598932.html 

 

代码:

Teacher.java

 1 package com.proc.pojo;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Student {
 7     private Integer id;
 8     private String name;
 9     private Set<Teacher> teachers=new HashSet<Teacher>();
10     public Student() {
11     }
12     public Student(String name) {
13         this.name = name;
14     }
15     public Integer getId() {
16         return id;
17     }
18     public void setId(Integer id) {
19         this.id = id;
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public Set<Teacher> getTeachers() {
28         return teachers;
29     }
30     public void setTeachers(Set<Teacher> teachers) {
31         this.teachers = teachers;
32     }
33     @Override
34     public String toString() {
35         return "Student [id=" + id + ", name=" + name + "]";
36     }
37     
38 }

 

Student.java

 1 package com.proc.pojo;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Teacher {
 7     private Integer id;
 8     private String name;
 9     private Set<Student> students=new HashSet<Student>();
10     public Teacher() {
11     }
12     public Teacher(String name) {
13         super();
14         this.name = name;
15     }
16     public Integer getId() {
17         return id;
18     }
19     public void setId(Integer id) {
20         this.id = id;
21     }
22     public String getName() {
23         return name;
24     }
25     public void setName(String name) {
26         this.name = name;
27     }
28     public Set<Student> getStudents() {
29         return students;
30     }
31     public void setStudents(Set<Student> students) {
32         this.students = students;
33     }
34     @Override
35     public String toString() {
36         return "Teacher [id=" + id + ", name=" + name + "]";
37     }
38     
39 }

 

Student.hbm.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="com.proc.pojo">
 6     <class name="Student" table="t_student">
 7         <id name="id" column="id" type="int">
 8             <generator class="native"></generator>
 9         </id>
10         <property name="name" column="name" type="string" length="20"></property>
11         <set name="teachers" table="t_student_teacher">
12             <key column="studentId"></key>
13             <many-to-many column="teacherId" class="Teacher"></many-to-many>
14         </set>
15     </class>
16 </hibernate-mapping>

 

Teacher.hbm.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping package="com.proc.pojo">
 6     <class name="Teacher" table="t_teacher">
 7         <id name="id" column="id" type="int">
 8             <generator class="native"></generator>
 9         </id>
10         <property name="name" column="name" type="string" length="20"></property>
11         <set name="students" table="t_student_teacher">
12             <key column="teacherId"></key>
13             <many-to-many column="studentId" class="Student"></many-to-many>
14         </set>
15     </class>
16 </hibernate-mapping>

 

测试代码:

 1 package com.proc.test;
 2 
 3 import java.util.Set;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.Transaction;
 8 import org.hibernate.cfg.Configuration;
 9 import org.junit.Test;
10 
11 
12 
13 import com.proc.pojo.Student;
14 import com.proc.pojo.Teacher;
15 
16 public class TestManageToManage {
17     private static SessionFactory factory=new Configuration()
18     .configure()
19     .addClass(Student.class)
20     .addClass(Teacher.class)
21     .buildSessionFactory();
22     
23     
24     @Test
25     public void set(){
26     
27     Session session=factory.openSession();
28     Transaction tran=session.beginTransaction();
29     
30     Student s1=new Student("张同学");
31     Student s2=new Student("王同学");
32     
33     Teacher t1=new Teacher("曹老师");
34     Teacher t2=new Teacher("曾老师");
35     
36     s1.getTeachers().add(t1);
37     s2.getTeachers().add(t1);
38     s2.getTeachers().add(t2);
39     
40     session.save(s1);    
41     session.save(s2);
42     session.save(t1);
43     session.save(t2);
44     
45     tran.commit();
46     session.close();
47     }
48     
49     @Test
50     public void get(){
51     
52     Session session=factory.openSession();
53     Transaction tran=session.beginTransaction();
54     
55     
56     Teacher t=session.get(Teacher.class, 2);
57     System.out.println(t);
58     Set<Student> set=t.getStudents();
59     
60     System.out.println(set);
61     System.out.println("=-------------------------");
62     Student s=session.get(Student.class, 2);
63     System.out.println(s);
64     System.out.println(s.getTeachers());
65     
66     tran.commit();
67     session.close();
68     }
69 }

 

以上是关于Hibernate多对多关系的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate多对一,多对多的表映射关系

Hibernate HQL详解

一口一口吃掉Hibernate——一对多单向关联映射

Hibernate学习8—Hibernate 映射关系(多对多)

Hibernate多对多关系映射(建表)

Hibernate-ORM:12.Hibernate中的多对多关联关系