Java进阶知识12 Hibernate多对多单向关联(Annotation+XML实现)

1、Annotation 注解版  

1.1、应用场景(Student-Teacher):当学生知道有哪些老师教,但是老师不知道自己教哪些学生时,可用单向关联

1.2、创建Teacher类和Student类

 1 package com.shore.model;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.GenerationType;
 6 import javax.persistence.Id;
 7 import javax.persistence.Table;
 8 
 9 /**
10  * @author DSHORE/2019-9-22
11  * 多对多,单向关联(注解版)
12  */
13 @Entity
14 @Table(name="anno_teacher")
15 public class Teacher {
16     private Integer id;
17     private String name;
18     private Integer age;
19     
20     @Id
21     @GeneratedValue(strategy=GenerationType.IDENTITY)
22     public Integer getId() {
23         return id;
24     }
25     public void setId(Integer id) {
26         this.id = id;
27     }
28     public String getName() {
29         return name;
30     }
31     public void setName(String name) {
32         this.name = name;
33     }
34     public Integer getAge() {
35         return age;
36     }
37     public void setAge(Integer age) {
38         this.age = age;
39     }
40 }

Student类

 1 package com.shore.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.GenerationType;
 9 import javax.persistence.Id;
10 import javax.persistence.JoinColumn;
11 import javax.persistence.JoinTable;
12 import javax.persistence.ManyToMany;
13 import javax.persistence.Table;
14 
15 /**
16  * @author DSHORE/2019-9-22
17  * 多对多,单向关联(注解版)
18  */
19 @Entity
20 @Table(name="anno_student") 
21 public class Student {
22     private Integer id;
23     private String number;
24     private Float sum;
25     private Set<Teacher> teachers = new HashSet<Teacher>();
26     
27     @Id
28     @GeneratedValue(strategy=GenerationType.IDENTITY)
29     public Integer getId() {
30         return id;
31     }
32     public void setId(Integer id) {
33         this.id = id;
34     }
35     public String getNumber() {
36         return number;
37     }
38     public void setNumber(String number) {
39         this.number = number;
40     }
41     public Float getSum() {
42         return sum;
43     }
44     public void setSum(Float sum) {
45         this.sum = sum;
46     }
47     
48     /**
49      * name:中间表的表名
50      * joinColumns:当前对象所对应的id
51      * inverseJoinColumns:对方对象所对应的id
52      */
53     @ManyToMany
54     @JoinTable(name="anno_student_teacher",
55                joinColumns=@JoinColumn(name="student_id"),
56                inverseJoinColumns=@JoinColumn(name="teacher_id"))
57     public Set<Teacher> getTeachers() {
58         return teachers;
59     }
60     public void setTeachers(Set<Teacher> teachers) {
61         this.teachers = teachers;
62     }
63 }

1.3、创建hibernate.cfg.xml核心配置文件

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <!-- Database connection settings -->
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
11         <property name="connection.username">root</property>
12         <property name="connection.password">123456</property>
13 
14         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
15         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
16         <property name="show_sql">true</property>
17         <property name="hbm2ddl.auto">create</property>
18 
19         <mapping class="com.shore.model.Teacher" />
20         <mapping class="com.shore.model.Student" />
21     </session-factory>
22 </hibernate-configuration>

1.4、开始测试

 1 package com.shore.test;
 2 
 3 import org.hibernate.cfg.AnnotationConfiguration;
 4 import org.hibernate.tool.hbm2ddl.SchemaExport;
 5 import org.junit.Test;
 6 
 7 /**
 8  * @author DSHORE/2019-9-22
 9  *
10  */
11 public class AnnotationTest {
12     @Test
13     public void test() {//简单测试,只创建表,不插入数据
14         new SchemaExport(new AnnotationConfiguration().configure()).create(
15                 false, true);
16     }
17 }

测试结果图:

      

    

 

 

2、XML版 的实现  

2.1、创建Teacher类和Student类

 1 package com.shore.model;
 2 
 3 /**
 4  * @author DSHORE/2019-9-22
 5  * 多对多,单向关联(xml版)
 6  */
 7 public class Teacher {
 8     private Integer id;
 9     private String name;
10     private Integer age;
11     
12     public Integer getId() {
13         return id;
14     }
15     public void setId(Integer id) {
16         this.id = id;
17     }
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     public Integer getAge() {
25         return age;
26     }
27     public void setAge(Integer age) {
28         this.age = age;
29     }
30 }

Student类

 1 package com.shore.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 /**
 7  * @author DSHORE/2019-9-22
 8  * 多对一,单向关联(xml版)
 9  */
10 public class Student {
11     private Integer id;
12     private String number;
13     private Float sum;
14     private Set<Teacher> teachers = new HashSet<Teacher>();
15     
16     public Integer getId() {
17         return id;
18     }
19     public void setId(Integer id) {
20         this.id = id;
21     }
22     public String getNumber() {
23         return number;
24     }
25     public void setNumber(String number) {
26         this.number = number;
27     }
28     public Float getSum() {
29         return sum;
30     }
31     public void setSum(Float sum) {
32         this.sum = sum;
33     }
34     public Set<Teacher> getTeachers() {
35         return teachers;
36     }
37     public void setTeachers(Set<Teacher> teachers) {
38         this.teachers = teachers;
39     }
40 }

2.2、创建 Teacher.hbm.xml 配置文件和 Student.hbm.xml 配置文件

 1 <?xml version="1.0"?>
 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         
 6 <hibernate-mapping package="com.shore.model">
 7     <class name="Teacher" table="xml_teacher">  
 8         <id name="id"> 
 9             <generator class="native"/>
10         </id>
11         <property name="name" type="java.lang.String"/>
12         <property name="age" type="java.lang.Integer"/>
13     </class>
14 </hibernate-mapping>

Student.hbm.xml 配置文件

 1 <?xml version="1.0"?>
 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         
 6 <hibernate-mapping package="com.shore.model">
 7     <class name="Student" table="xml_student">  
 8         <id name="id"> 
 9             <generator class="native"/>
10         </id>
11         <property name="number" type="java.lang.String"/>
12         <property name="sum" type="java.lang.Float"/>
13         
14         <set name="teachers" table="xml_student_teacher">
15             <key column="student_id"/> <!-- 当前对象所对应的id -->
16             <many-to-many class="com.shore.model.Teacher" column="teacher_id"/> <!-- 对方对象所对应的id -->
17         </set>
18     </class>
19 </hibernate-mapping>

2.3、创建hibernate.cfg.xml 核心配置文件

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <!-- Database connection settings -->
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
11         <property name="connection.username">root</property>
12         <property name="connection.password">123456</property>
13 
14         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
15         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
16         <property name="show_sql">true</property>
17         <property name="hbm2ddl.auto">create</property>
18 
19         <!-- <mapping class="com.shore.model.Teacher" />
20         <mapping class="com.shore.model.Student" /> -->
21         <mapping resource="com/shore/model/Teacher.hbm.xml" />
22         <mapping resource="com/shore/model/Student.hbm.xml" />
23     </session-factory>
24 </hibernate-configuration>

2.4、开始测试

 1 package com.shore.test;
 2 
 3 import org.hibernate.cfg.Configuration;
 4 import org.hibernate.tool.hbm2ddl.SchemaExport;
 5 import org.junit.Test;
 6 
 7 /**
 8  * @author DSHORE/2019-9-22
 9  *
10  */
11 public class XMLTest {
12     @Test
13     public void test() {//简单测试,只创建表,不插入数据
14                     //xml版,这里用的是Configuration()方法。
15         new SchemaExport(new Configuration().configure()).create(
16                 false, true);
17     }
18 }

测试结果图:

    

    

 

 

Hibernate一对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545058.html
Hibernate一对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545077.html

Hibernate多对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553213.html
Hibernate一对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553215.html
Hibernate一对多多对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11560433.html

Hibernate多对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568536.html
Hibernate多对多双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568963.html

 

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/11568536.html

版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2019-09-22 18:12  DSHORE  阅读(253)  评论(0编辑  收藏  举报