05.Hibernate多对多关联
1.数据库表的多对多关系
CREATE TABLE tb_student
(
id bigint NOT NULL auto_increment COMMENT 'ID',
no varchar(10) NOT NULL COMMENT '学号',
name varchar(50) NOT NULL COMMENT '姓名',
sex char(1) NOT NULL COMMENT '性别',
birthday datetime COMMENT '出生日期',
class_id bigint COMMENT '班级ID',
PRIMARY KEY (id)
) COMMENT = '学生信息表';
CREATE TABLE tb_teacher
(
id bigint NOT NULL auto_increment COMMENT 'ID',
no varchar(10) NOT NULL COMMENT '教师编号',
name varchar(50) NOT NULL COMMENT '教师姓名',
sex char(1) NOT NULL COMMENT '教师性别',
job_title varchar(50) NOT NULL COMMENT '职称',
PRIMARY KEY (id)
) COMMENT = '教师信息表';
CREATE TABLE tb_student_teacher
(
student_id bigint NOT NULL COMMENT '学生ID',
teacher_id bigint NOT NULL COMMENT '教师ID',
PRIMARY KEY (student_id,teacher_id)
) COMMENT = '学生教师关系表';
-- 可选的外键约束
ALTER TABLE tb_student_teacher ADD CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES tb_student (id);
ALTER TABLE tb_student_teacher ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES tb_teacher (id);
package model;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Student
{
private Long id;
private String no;
private String name;
private String sex;
private Date birthday;
private Long classId;
private Set<Teacher> teachers=new HashSet<Teacher>();
@Override
public String toString()
{
return "Student [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", classId=" + classId + ", teachers=" + teachers.size() + "]";
}
//省略setter、getter...
}
package model;
import java.util.HashSet;
import java.util.Set;
public class Teacher
{
private Long id;
private String no;
private String name;
private String sex;
private String job_title;
private Set<Student> students=new HashSet<Student>();
@Override
public String toString()
{
return "Teacher [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", job_title=" + job_title + ", students=" + students.size() + "]";
}
//省略setter、getter...
}
2.单向多对多关系
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="students" table="tb_student_teacher" cascade="save-update">
<key column="teacher_id"/>
<many-to-many class="model.Student" column="student_id" />
</set>
</class>
</hibernate-mapping>
- 由于实体类使用的是Set集合所以使用set元素配置关联关系,根据实体类的集合类型也可以对应的使用idbag、list、map元素。
- 由于tb_student表与tb_teacher表的多对多关系是通过tb_student_teacher表来保存的,所以set元素的table属性设置成table="tb_student_teacher"。
- set元素的key子元素设置column="teacher_id",表示tb_student_teacher表中参照tb_teacher表的外键设置为teacher_id。
- set元素的many-to-many子元素设置class="model.Student",表示Teacher类的students属性的类型,而其column="student_id",表示tb_student_teacher表中参照model.Student类型对应的表的外键设置为student_id。
- 所以set元素的cascade属性设置成save-update是很合理的,不允许把cascade设置成all、delete、all-delete-orphands,假如删除一个Teacher对象时还会级联删除与他关联的所有Student对象,由于Student对象可能还会与其他的Teacher对象关联,因此当Hibernate执行级联删除时会违反数据库的外键参照完整性!
public static void main(String[] args)
{
Teacher teacher;
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
System.out.println("连接数据库");
Session session = sf.openSession();
teacher =(Teacher) session.get(Teacher.class, new Long(1));
System.out.println(teacher);
System.out.println(teacher.getStudents());
session.close();
System.exit(0);
}
连接数据库
Hibernate:
select
teacher0_.id as id1_7_0_,
teacher0_.no as no2_7_0_,
teacher0_.name as name3_7_0_,
teacher0_.sex as sex4_7_0_,
teacher0_.job_title as job_titl5_7_0_
from
tb_teacher teacher0_
where
teacher0_.id=?
Hibernate:
select
students0_.teacher_id as teacher_1_7_0_,
students0_.student_id as student_2_6_0_,
student1_.id as id1_5_1_,
student1_.no as no2_5_1_,
student1_.name as name3_5_1_,
student1_.sex as sex4_5_1_,
student1_.birthday as birthday5_5_1_,
student1_.class_id as class_id6_5_1_
from
tb_student_teacher students0_
inner join
tb_student student1_
on students0_.student_id=student1_.id
where
students0_.teacher_id=?
Teacher [id=1, no=000001, name=教师1, sex=女, job_title=初级教师, students=1]
Hibernate:
select
teachers0_.student_id as student_2_5_0_,
teachers0_.teacher_id as teacher_1_6_0_,
teacher1_.id as id1_7_1_,
teacher1_.no as no2_7_1_,
teacher1_.name as name3_7_1_,
teacher1_.sex as sex4_7_1_,
teacher1_.job_title as job_titl5_7_1_
from
tb_student_teacher teachers0_
inner join
tb_teacher teacher1_
on teachers0_.teacher_id=teacher1_.id
where
teachers0_.student_id=?
[Student [id=1, no=000001, name=学生1, sex=男, birthday=2015-01-27, classId=41, teachers=1]]
关闭数据库
3.双向多对多关系
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
<set name="teachers" table="tb_student_teacher" cascade="save-update" inverse="true">
<key column="student_id"/>
<many-to-many class="model.Teacher" column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="students" table="tb_student_teacher" cascade="save-update" inverse="false">
<key column="teacher_id"/>
<many-to-many class="model.Student" column="student_id" />
</set>
</class>
</hibernate-mapping>
4.使用组件类集合映射多对多关联
package model;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Student
{
private Long id;
private String no;
private String name;
private String sex;
private Date birthday;
private Long classId;
private Set<TeacherAndStudent> teacherAndStudents = new HashSet<TeacherAndStudent>();
@Override
public String toString()
{
return "Student [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", classId=" + classId + "]";
}
// 省略setter、getter...
}
package model;
import java.util.HashSet;
import java.util.Set;
public class Teacher
{
private Long id;
private String no;
private String name;
private String sex;
private String job_title;
private Set<TeacherAndStudent> teacherAndStudents = new HashSet<TeacherAndStudent>();
@Override
public String toString()
{
return "Teacher [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", job_title=" + job_title + "]";
}
// 省略setter、getter...
}
package model;
public class TeacherAndStudent
{
private Teacher teacher;
private Student student;
@Override
public String toString()
{
return "TeacherAndStudent [teacher=" + teacher + ", student=" + student + "]";
}
// 省略setter、getter...
}
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
<set name="teacherAndStudents" table="tb_student_teacher" cascade="save-update" inverse="true">
<key column="student_id" />
<composite-element class="TeacherAndStudent">
<parent name="student" />
<many-to-one name="teacher" class="Teacher" column="teacher_id"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="teacherAndStudents" table="tb_student_teacher" cascade="save-update" inverse="false">
<key column="teacher_id"/>
<composite-element class="TeacherAndStudent">
<parent name="teacher"/>
<many-to-one name="student" class="Student" column="student_id"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
- 在Hibernate中使用composite-element元素映射组件类。
- composite-element元素下的parent元素用来设置当前组件所属的整体类。
- composite-element元素下还可以使用many-to-one元素和property元素等。
public static void main(String[] args)
{
Teacher teacher;
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
System.out.println("连接数据库");
Session session = sf.openSession();
teacher =(Teacher) session.get(Teacher.class, new Long(1));
System.out.println(teacher);
System.out.println(teacher.getTeacherAndStudents());
session.close();
System.exit(0);
}
连接数据库
Hibernate:
select
teacher0_.id as id1_7_0_,
teacher0_.no as no2_7_0_,
teacher0_.name as name3_7_0_,
teacher0_.sex as sex4_7_0_,
teacher0_.job_title as job_titl5_7_0_
from
tb_teacher teacher0_
where
teacher0_.id=?
Teacher [id=1, no=000001, name=教师1, sex=女, job_title=初级教师]
Hibernate:
select
teacherand0_.teacher_id as teacher_1_7_0_,
teacherand0_.student_id as student_2_6_0_,
student1_.id as id1_5_1_,
student1_.no as no2_5_1_,
student1_.name as name3_5_1_,
student1_.sex as sex4_5_1_,
student1_.birthday as birthday5_5_1_,
student1_.class_id as class_id6_5_1_
from
tb_student_teacher teacherand0_
left outer join
tb_student student1_
on teacherand0_.student_id=student1_.id
where
teacherand0_.teacher_id=?
[TeacherAndStudent [teacher=Teacher [id=1, no=000001, name=教师1, sex=女, job_title=初级教师], student=Student [id=1, no=000001, name=学生1, sex=男, birthday=2015-01-27, classId=41]]]
Hibernate:
delete
from
tb_student_teacher
where
teacher_id=?
Hibernate:
insert
into
tb_student_teacher
(teacher_id, student_id)
values
(?, ?)
关闭数据库
5.把多对多分解成两个一对多
CREATE TABLE tb_teacher_student
(
id bigint NOT NULL auto_increment COMMENT 'ID',
student_id bigint COMMENT '学生ID',
teacher_id bigint COMMENT '教师ID',
PRIMARY KEY (id)
) COMMENT = '教师学生关系表';
package model;
public class StudentTeacher
{
private Long id;
private Teacher teacher;
private Student student;
@Override
public String toString()
{
return "StudentTeacher [id=" + id + ", teacher=" + teacher + ", student=" + student + "]";
}
// 省略setter、getter...
}
package model;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Student
{
private Long id;
private String no;
private String name;
private String sex;
private Date birthday;
private Long classId;
private Set<StudentTeacher> studentTeachers = new HashSet<StudentTeacher>();
@Override
public String toString()
{
return "Student [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", classId=" + classId + "]";
}
// 省略setter、getter...
}
package model;
import java.util.HashSet;
import java.util.Set;
public class Teacher
{
private Long id;
private String no;
private String name;
private String sex;
private String job_title;
private Set<StudentTeacher> studentTeachers = new HashSet<StudentTeacher>();
@Override
public String toString()
{
return "Teacher [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", job_title=" + job_title + "]";
}
// 省略setter、getter...
}
<hibernate-mapping package="model">
<class name="StudentTeacher" table="tb_teacher_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<many-to-one name="teacher" column="teacher_id" class="Teacher"/>
<many-to-one name="student" column="student_id" class="Student"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="studentTeachers" inverse="true" cascade="save-update">
<key column="teacher_id" />
<one-to-many class="StudentTeacher"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
<set name="studentTeachers" inverse="true" cascade="save-update">
<key column="student_id" />
<one-to-many class="StudentTeacher"/>
</set>
</class>
</hibernate-mapping>
......省略打开session的代码
try
{
teacher =(Teacher) session.get(Teacher.class, new Long(1));
student = (Student) session.get(Student.class, new Long(1));
StudentTeacher st=new StudentTeacher();
st.setStudent(student);
st.setTeacher(teacher);
student.getStudentTeachers().add(st);
session.update(student);
transaction.commit();
}
......省略关闭session的代码
连接数据库
Hibernate:
........ 省略若干获取关联对象的查询语句Hibernate: insert into tb_teacher_student (teacher_id, student_id) values (?, ?)
关闭数据库
6.相关配置详解
<set name="EntityClassName"
access="field|property|ClassName"
collection-type="collection-type"
schema="schema"
catalog="catalog"
check="arbitrary sql check condition"
table="TableName"
subselect="SQL expression"
where="arbitrary sql where condition"
sort="unsorted"
optimistic-lock="false|true"
inverse="false|true"
fetch="join|select"
batch-size="5"
cascade="all|none|save-update|delete"
lazy="false|true"
mutable="false|true"
outer-join="false|true"
order-by="arbitrary sql order by condition"
embed-xml="false|true"
persister="PersisterClass"
node="element-name"/>
- name:实体类属性名。
- access:默认的实体类属性访问模式,取值为property表示访问getter、setter方法间接访问实体类字段,取值为field表示直接访问实体类字段(类成员变量)。
- collection-type:
- schema:数据库schema。
- catalog:数据库catalog。
- check:这是一个SQL表达式,用于为自动生成的schema添加多行约束检查。
- table:此集合里的实体类对应的数据库表名。
- subselect:一个SQL子查询,它将一个不可变并且只读的实体映射到一个数据库的子查询。
- where:一个SQL查询的where条件,获取这个关联类的对象时会一直增加这个条件。
- sort:用来定义集合的排序规则,sort="natural"表示使用对象的comparaTo()方法排序。
- optimistic-lock:指定这个属性在做更新时是否需要获得乐观锁定,默认为true。
- inverse:当设置inverse="true"时,Hibernate将根此集合里的实体类类型的关联属性维护关联关系,默认值false。
- fetch:参数指定了关联对象抓取的方式是select查询还是join查询,默认为select。fetch="join"等同于outer-join="true",fetch="select"等同于outer-join="false"。
- batch-size:用于设置批次操作的SQL语句的数量,默认为1。
- cascade:指明哪些操作会从父对象级联到关联的对象。
- lazy:是否采用延迟加载策略。
- mutable:此集合里的实体类是否会发生改变,如果类实例对应的数据库表记录不会发生更新,可将其设为false,适用于单纯的Insert操作不使用update操作。
- outer-join:设置Hibernate是否使用外连接获取关联的数据,设置成true可以减少SQL语句的条数。
- order-by:一个SQL查询的order by条件,获取这个关联类的对象时会一直增加这个条件。
- embed-xml:如果embed-xml="true",则对应于被关联实体或值类型的集合的XML树将直接嵌入拥有这些关联的实体的XML树中,默认值为true。
- persister:指定持久化实现类,通过指定持久化类,我们可以实现自定义的持久化方法。持久化类为ClassPersister接口的实现。
- node:配置说明。
<composite-element class="ClassName
" node="element-name"><parent name="PropertyName
" /><property name="PropertyName
"></property><many-to-one />
</composite-element>
- class:用于设置组件的类型。
- node:配置说明。
- parent name:组件所属的实体类类型,值一般是组件类本身的实体类属性。
- property:组件类属性名。
- many-to-one:用于配置组件类的关联关系,不仅能使用many-to-one元素,还可使用其他的关联映射配置。
-------------------------------------------------------------------------------------------------------------------------------
05.Hibernate多对多关联的更多相关文章
- hibernate多对多关联映射
关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...
- atitit.atitit.hb many2one relate hibernate 多对一关联配置..
atitit.atitit.hb many2one relate hibernate 多对一关联配置.. 1. 多对一单向 @ManyToOne 1 1. 其中@JoinColumn 注解 2 2. ...
- Hibernate多对多关联
多对多关联: 示例:Teacher和Student,一个Teacher可以教很多student,一个Student也可以被很多teacher教 多对多单向关联 Teacher知道自己教了哪些学生, ...
- Hibernate多对多关联映射的HQL中的in条件查询问题
群里有朋友求解一个问题,高分求一条HQL多对多查询语句 . 问题描述见 http://topic.csdn.net/u/20090621/16/4eac6fe0-bf3e-422e-a697-f758 ...
- Hibernate 多对一关联查询
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5598269.html 一.单向多对一和双向多对一的区别 如果只 ...
- Hibernate 多对多关联映射, 中间表加查询条件
package com.coracle.yk.xpo.po.base; import java.util.Date; import java.util.HashSet; import java.uti ...
- Hibernate 多对多关联Demo
以学生[Student ]与课程[Course ]之间的关系为例: //Course .java public class Course implements Serializable { priva ...
- hibernate 多对一关联
(转自尚学堂教学内容) 注解多对一: package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.pers ...
- Hibernate 多对多关联查询条件使用
from Brand as b inner join fetch b.styles as s where s.styleId=?
随机推荐
- css中li、a、span行内强制不换行
li.a.span行内强制不换行:white-space:nowrap; 没有之前的效果 加上white-space:nowrap;后
- Hbase负载均衡流程以及源码
hmater负责把region均匀到各个region server .hmaster中有一个线程任务是专门处理负责均衡的,默认每隔5分钟执行一次. 每次负载均衡操作可以分为两步: 生成负载均衡计划表 ...
- JavaService应用中的注意事项
最近有个技术需求,要把已写好的Java程序注册成Windows服务,网上搜了两个快捷办法,一个是Java Service Wrapper,这是个收费的第三方组件,免费的版本还没有适合64位Win7系统 ...
- mysql之数据库基本概念(mysql学习笔记一)
数据库系统 数据库管理系统(DBMS)+数据库(DATABASE)(+数据库管理员) DBS=dbms+db 定义: 大量信息进行管理的高效解决方案,按照数据结构来组织.存储和管理数据的仓库 关系 ...
- 如何将两个列表变成一个python字典
一个列表是 index = [0, 1, 2, 3, 4, 5, 6] 另一个是 day = ['1', '2', '3', '4', '5', '6', '7' ] 可以使用dict(zip(ind ...
- adb 修改system文件
1. $ adb push SecureSetting.apk /sdcard/ // 上传要安装的文件,为安装做准备. 2. $ adb shell 3. $ su // 切换到 ...
- C# 验证IP是否正确简易方法 源代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- JSTL实现分页
JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1. ...
- oracle 归档/非归档
1.查看oralce是归档模式还是非归档模式 SQL> select name,log_mode from v$database; NAME LOG_MODE------------------ ...
- OpenGL完整实例
结合上一节的内容,分享完整代码. 先画一个cube,然后通过OnGestureListener去触发onFling使它旋转起来. OnGestureListener相关的方法我已经都加了注释,可以参考 ...