Java进阶知识12 Hibernate多对多双向关联(Annotation+XML实现)
1、Annotation 注解版
1.1、应用场景(Student-Teacher):当学生知道有哪些老师教,老师也知道自己教哪些学生时,可用双向关联
1.2、创建Teacher类和Student类
package com.shore.model; import java.util.HashSet;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table; /**
* @author DSHORE/2019-9-22
* 多对多,双向关联(注解版)
*/
@Entity
@Table(name="anno_teacher")
public class Teacher {
private Integer id;
private String name;
private Integer age;
private Set<Student> students = new HashSet<Student>(); @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} @ManyToMany(mappedBy="teachers") //和单向的多对多,只多了这一步。 只要是双向关联,必须加上这句话:(mappedBy="xxxxxx")
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Student类
package com.shore.model; import java.util.HashSet;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table; /**
* @author DSHORE/2019-9-22
* 多对一,双向关联(注解版)
*/
@Entity
@Table(name="anno_student")
public class Student {
private Integer id;
private String number;
private Float sum;
private Set<Teacher> teachers = new HashSet<Teacher>(); @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Float getSum() {
return sum;
}
public void setSum(Float sum) {
this.sum = sum;
} /**
* name:中间表的表名
* joinColumns:当前对象所对应的id
* inverseJoinColumns:对方对象所对应的id
*/
@ManyToMany
@JoinTable(name="anno_student_teacher",
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="teacher_id"))
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
1.3、创建hibernate.cfg.xml核心配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property> <mapping class="com.shore.model.Teacher" />
<mapping class="com.shore.model.Student" />
</session-factory>
</hibernate-configuration>
1.4、开始测试
package com.shore.test; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test; /**
* @author DSHORE/2019-9-22
*
*/
public class AnnotationTest {
@Test
public void test() {//简单测试,只创建表,不插入数据
//注解版,这里用AnnotationConfiguration()方法。
new SchemaExport(new AnnotationConfiguration().configure()).create(
false, true);
}
}
测试结果图:



表结构虽然看起来和单向多对多关联的没什么区别,但进行CRUD操作时,就会发现他们的区别了,此处不做解析
2、XML版 的实现
2.1、创建Teacher类和Student类
package com.shore.model; import java.util.HashSet;
import java.util.Set; /**
* @author DSHORE/2019-9-22
* 多对多,双向关联(xml版)
*/
public class Teacher {
private Integer id;
private String name;
private Integer age;
private Set<Student> students = new HashSet<Student>(); public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Student类
package com.shore.model; import java.util.HashSet;
import java.util.Set; /**
* @author DSHORE/2019-9-22
* 多对多,双向关联(xml版)
*/
public class Student {
private Integer id;
private String number;
private Float sum;
private Set<Teacher> teachers = new HashSet<Teacher>(); public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Float getSum() {
return sum;
}
public void setSum(Float sum) {
this.sum = sum;
}
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
2.2、创建 Teacher.hbm.xml 配置文件和 Student.hbm.xml 配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.shore.model">
<class name="Teacher" table="xml_teacher">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="age" type="java.lang.Integer"/> <!-- 多对多 双向关联 -->
<set name="students" table="xml_student_teacher">
<key column="teacher_id"/> <!-- 当前对象所对应的id -->
<many-to-many class="com.shore.model.Student" column="student_id"/> <!-- 对方对象所对应的id -->
</set>
</class>
</hibernate-mapping>
Student.hbm.xml 配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.shore.model">
<class name="Student" table="xml_student">
<id name="id">
<generator class="native"/>
</id>
<property name="number" type="java.lang.String"/>
<property name="sum" type="java.lang.Float"/> <!-- 多对多 双向关联 -->
<set name="teachers" table="xml_student_teacher">
<key column="student_id"/> <!-- 当前对象所对应的id -->
<many-to-many class="com.shore.model.Teacher" column="teacher_id"/> <!-- 对方对象所对应的id -->
</set>
</class>
</hibernate-mapping>
2.3、创建hibernate.cfg.xml 核心配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property> <!-- <mapping class="com.shore.model.Teacher" />
<mapping class="com.shore.model.Student" /> -->
<mapping resource="com/shore/model/Teacher.hbm.xml" />
<mapping resource="com/shore/model/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.4、开始测试
package com.shore.test; import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test; /**
* @author DSHORE/2019-9-22
*
*/
public class XMLTest {
@Test
public void test() {//简单测试,只创建表,不插入数据
//xml版,这里用的是Configuration()方法。
new SchemaExport(new Configuration().configure()).create(
false, true);
}
}
测试结果图:



表结构虽然看起来和单向多对多关联的没什么区别,但进行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 版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |
Java进阶知识12 Hibernate多对多双向关联(Annotation+XML实现)的更多相关文章
- Java进阶知识11 Hibernate多对多单向关联(Annotation+XML实现)
1.Annotation 注解版 1.1.应用场景(Student-Teacher):当学生知道有哪些老师教,但是老师不知道自己教哪些学生时,可用单向关联 1.2.创建Teacher类和Student ...
- Java进阶知识08 Hibernate多对一单向关联(Annotation+XML实现)
1.Annotation 注解版 1.1.在多的一方加外键 1.2.创建Customer类和Order类 package com.shore.model; import javax.persisten ...
- Java进阶知识09 Hibernate一对多单向关联(Annotation+XML实现)
1.Annotation 注解版 1.1.在一的一方加Set 1.2.创建Customer类和Order类 package com.shore.model; import java.util.Hash ...
- Java进阶知识05 Hibernate联合主键之Annotation(注解)和XML实现方式
1.Hibernate联合主键(Annotation实现) 1.1.单列主键 1.1.1.为什么要有主键? //唯一确定一条记录 1.1.2.一个表能否有多个主键? //不能 1.1.3. ...
- Java进阶知识10 Hibernate一对多_多对一双向关联(Annotation+XML实现)
本文知识点(目录): 1.Annotation 注解版(只是测试建表) 2.XML版 的实现(只是测试建表) 3.附录(Annotation 注解版CRUD操作)[注解版有个问题:插入值时 ...
- Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)
1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...
- Java进阶知识06 Hibernate一对一单向外键关联(Annotation+XML实现)
1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...
- hibernate多对一双向关联
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- Hibernate多对多双向关联的配置
Hibernate的双向多对多关联有两种配置方法:那我们就来看看两种方案是如何配置的. 一.创建以各自类为类型的集合来关联 1.首先我们要在两个实体类(雇员<Emploee>.工程< ...
随机推荐
- c++ 之private /protect/ public
C++中public.protect.private的访问权限控制 访问权限 一个类的public成员变量.成员函数,可以通过类的成员函数.类的实例变量进行访问 一个类的protected成员变量.成 ...
- 解决python无法安装mysql数据库问题
解决python无法安装mysql数据库问题: pip install pymysql[使用这个命令来安装]
- Prometheus Operator 的安装
Prometheus Operator 的安装 接下来我们用自定义的方式来对 Kubernetes 集群进行监控,但是还是有一些缺陷,比如 Prometheus.AlertManager 这些组件服务 ...
- PowerShell 反弹渗透技巧
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能,并且与现有的WSH保持向后兼容,因此它的脚本程序不仅能访 ...
- China Union Pay helper
static string proxyIpAddress = AppConfig.GetProxyIpAddress; static string proxyUserName = AppConfig. ...
- 11.SpringMVC注解式开发-处理器方法的返回值
处理器方法的返回值 使用@Controller 注解的处理器的处理器方法,其返回值常用的有四种类型 1.ModelAndView 2.String 3.void 4.自定义类型对象 1.返回Model ...
- beego从入门到弃坑(一)
最近由于要写课程设计的原因,我便开始一边学习beego,一边开始用它写一个小型的管理系统.但是只有你真正的去用的时候,才会发现这个框架巨坑,他是第一个让我写出了心里阴影的框架,也是第一个让我写着写 ...
- JavaSpring【三、Bean】
配置项 id bean的标识 class bean的类全名 scope bean的作用域 constructor-arg 构造注入 properties 设值注入 autowire 装配模式 lazy ...
- Maven新建项目出现 Could not calculate build plan:plugin 错误解决办法
删除本地.m2仓库中 org.apache.maven.plugins:maven-resources-plugin所在目录. 然后右击项目 Maven->Update Project-> ...
- Swift Review总结一:从 Swift Style 开始
最近凑了几个热心的小伙伴写一些Swift的新手demo(两周后应该能和大家见面了),我参与了review.于是借demo里的代码总结一下新手写Swift要注意的问题,尤其是从oc转到用swift写的开 ...