Java进阶知识13 Hibernate多对多双向关联(Annotation+XML实现)
1、Annotation 注解版
1.1、应用场景(Student-Teacher):当学生知道有哪些老师教,老师也知道自己教哪些学生时,可用双向关联
1.2、创建Teacher类和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.ManyToMany; 11 import javax.persistence.Table; 12 13 /** 14 * @author DSHORE/2019-9-22 15 * 多对多,双向关联(注解版) 16 */ 17 @Entity 18 @Table(name="anno_teacher") 19 public class Teacher { 20 private Integer id; 21 private String name; 22 private Integer age; 23 private Set<Student> students = new HashSet<Student>(); 24 25 @Id 26 @GeneratedValue(strategy=GenerationType.IDENTITY) 27 public Integer getId() { 28 return id; 29 } 30 public void setId(Integer id) { 31 this.id = id; 32 } 33 public String getName() { 34 return name; 35 } 36 public void setName(String name) { 37 this.name = name; 38 } 39 public Integer getAge() { 40 return age; 41 } 42 public void setAge(Integer age) { 43 this.age = age; 44 } 45 46 @ManyToMany(mappedBy="teachers") //和单向的多对多,只多了这一步。 只要是双向关联,必须加上这句话:(mappedBy="xxxxxx") 47 public Set<Student> getStudents() { 48 return students; 49 } 50 public void setStudents(Set<Student> students) { 51 this.students = students; 52 } 53 }
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 //注解版,这里用AnnotationConfiguration()方法。 15 new SchemaExport(new AnnotationConfiguration().configure()).create( 16 false, true); 17 } 18 }
测试结果图:
表结构虽然看起来和单向多对多关联的没什么区别,但进行CRUD操作时,就会发现他们的区别了,此处不做解析
2、XML版 的实现
2.1、创建Teacher类和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 Teacher { 11 private Integer id; 12 private String name; 13 private Integer age; 14 private Set<Student> students = new HashSet<Student>(); 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 Integer getAge() { 29 return age; 30 } 31 public void setAge(Integer age) { 32 this.age = age; 33 } 34 public Set<Student> getStudents() { 35 return students; 36 } 37 public void setStudents(Set<Student> students) { 38 this.students = students; 39 } 40 }
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 14 <!-- 多对多 双向关联 --> 15 <set name="students" table="xml_student_teacher"> 16 <key column="teacher_id"/> <!-- 当前对象所对应的id --> 17 <many-to-many class="com.shore.model.Student" column="student_id"/> <!-- 对方对象所对应的id --> 18 </set> 19 </class> 20 </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 <!-- 多对多 双向关联 --> 15 <set name="teachers" table="xml_student_teacher"> 16 <key column="student_id"/> <!-- 当前对象所对应的id --> 17 <many-to-many class="com.shore.model.Teacher" column="teacher_id"/> <!-- 对方对象所对应的id --> 18 </set> 19 </class> 20 </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 }
测试结果图:
表结构虽然看起来和单向多对多关联的没什么区别,但进行CRUD操作时,就会发现他们的区别了,此处不做解析
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/11568963.html 版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |