博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate的fetch
阅读量:6039 次
发布时间:2019-06-20

本文共 6573 字,大约阅读时间需要 21 分钟。

 

hibernate抓取策略fetch具体解释

一、hibernate抓取策略(单端代理的批量抓取fetch=select(默认)/join)
測试用例:
Student student = (Student)session.get(Student.class, 1);
System.out.println(student.getName());
System.out.println(student.getClasses().getName());

1)保持默认,同fetch="select",如:

<many-to-one name="classes" column="classesid" fetch="select"/>

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合

hibernate抓取策略fetch具体解释

运行结果:2条语句

Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?

学生1
Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes_join classes0_ where classes0_.id=?
高一(1)班
2)设置fetch="join",如:
<many-to-one name="classes" column="classesid" fetch="join"/>

fetch="join",hibernate会通过select语句使用外连接来载入其关联实体或集合

hibernate抓取策略fetch具体解释

此时lazy会失效

运行结果:一条join语句

Hibernate: select student0_.id as id1_1_, student0_.name as name1_1_, student0_.class_id as class3_1_1_, classes1_.id as id0_0_, classes1_.name as name0_0_ from student_join student0_ left outer join classes_join classes1_ on student0_.class_id=classes1_.id where student0_.id=?

学生1
高一(1)班
二、hibernate抓取策略(集合代理的批量抓取,fetch=select(默认)/join/subselect)

測试用例:

Classes c = (Classes) session.load(Classes.class, new Integer(1));

System.out.println("Class.name=" + c.getName());
Set stuSet = c.getStudents();
System.out.println(stuSet.size());
if(stuSet != null && !stuSet.isEmpty()){
for(Iterator it = stuSet.iterator(); it.hasNext();){
Student s = (Student) it.next();
System.out.println("student.name=" + s.getName());
}
}

1)hibernate抓取策略fetch具体解释

保持默认,同fetch="select",如:
<set name="students" inverse="true" fetch="select">

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合

測试结果:2条独立的查询语句

Hibernate: select classes0_.id as id0_0_, classes0_.name as name0_0_ from classes_join classes0_ where classes0_.id=?

Class.name=高一(1)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
9
student.name=学生7
student.name=学生3
student.name=学生1
student.name=学生8
student.name=学生2
student.name=学生4
student.name=学生5
student.name=学生9
student.name=学生6

(2)设置fetch="join",如:

<set name="students" inverse="true" fetch="join">

fetch="join",hibernate会通过select语句使用外连接来载入其关联实体或集合

此时lazy会失效

測试结果:1条独立的join查询语句

Hibernate: select classes0_.id as id0_1_, classes0_.name as name0_1_, students1_.class_id as class3_3_, students1_.id as id3_, students1_.id as id1_0_, students1_.name as name1_0_, students1_.class_id as class3_1_0_ from classes_join classes0_ left outer join student_join students1_ on classes0_.id=students1_.class_id where classes0_.id=?

Class.name=高一(1)班
9
student.name=学生6
student.name=学生4
student.name=学生9
student.name=学生7
student.name=学生2
student.name=学生3
student.name=学生8
student.name=学生1
student.name=学生5
(3)设置fetch="subselect",如:用在查询语句中
<set name="students" inverse="true" fetch="subselect">

fetch="subselect",另外发送一条select语句抓取在前面查询到的全部实体对象的关联集合

測试用例:

List classList = session.createQuery("from Classes where id in (1,2,3)").list();

for(Iterator iter = classList.iterator(); iter.hasNext();){
Classes c = (Classes)iter.next();
System.out.println("Class.name=" + c.getName());
Set stuSet = c.getStudents();
System.out.println(stuSet.size());
if(stuSet != null && !stuSet.isEmpty()){
for(Iterator it = stuSet.iterator(); it.hasNext();){
Student s = (Student) it.next();
System.out.println("student.name=" + s.getName());
}
}
}

当不设fetch="subselect" ,即:<set name="students" inverse="true">,结果例如以下:

运行了3条查询语句

Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from classes_join classes0_ where classes0_.id in (1 , 2 , 3)

Class.name=高一(1)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
9
student.name=学生8
student.name=学生5
student.name=学生3
student.name=学生9
student.name=学生7
student.name=学生1
student.name=学生4
student.name=学生6
student.name=学生2
Class.name=高一(2)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
4
student.name=学生3
student.name=学生4
student.name=学生1
student.name=学生2
Class.name=高一(3)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id=?
0

当不设fetch="subselect" ,即:<set name="students" inverse="true" fetch="subselect">,结果例如以下:

运行了1条查询语句(嵌套子查询)

Hibernate: select classes0_.id as id0_, classes0_.name as name0_ from classes_join classes0_ where classes0_.id in (1 , 2 , 3)

Class.name=高一(1)班
Hibernate: select students0_.class_id as class3_1_, students0_.id as id1_, students0_.id as id1_0_, students0_.name as name1_0_, students0_.class_id as class3_1_0_ from student_join students0_ where students0_.class_id in (select classes0_.id from classes_join classes0_ where classes0_.id in (1 , 2 , 3))

 

 

------------------------------------------------------

总结:

hibernate抓取策略(单端代理的批量抓取)

保持默认,同fetch="select",如:

<many-to-one name="classes" column="classesid" fetch="select"/>

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合

 

2.hibernate抓取策略(单端代理的批量抓取)

设置fetch="join",如:

<many-to-one name="classes" column="classesid" fetch="join"/>

fetch="join",hibernate会通过select语句使用外连接来载入其关联实体或集合

此时lazy会失效

 

3.hibernate抓取策略(集合代理的批量抓取)

保持默认,同fetch="select",如:

<set name="students" inverse="true" cascade="all" fetch="select">

fetch="select",另外发送一条select语句抓取当前对象关联实体或集合

 

4.hibernate抓取策略(集合代理的批量抓取)

设置fetch="join",如:

<set name="students" inverse="true" cascade="all" fetch="join">

fetch="join",hibernate会通过select语句使用外连接来载入其关联实体或集合

此时lazy会失效

5.hibernate抓取策略(集合代理的批量抓取)

设置fetch="subselect",如:

<set name="students" inverse="true" cascade="all" fetch="subselect">

fetch="subselect",另外发送一条select语句抓取在前面查询到的全部实体对象的关联集合

 

6.hibernate抓取策略,,batch-szie在<class>上的应用

batch-size属性,能够批量载入实体类,參见:Classes.hbm.xml

<class name="Classes" table="t_classes" batch-size="3">

当查classes对象时发出9条hql语句配置过后batch-size=3后会之发9/3=3条hql语句,提高性能

 

7.hibernate抓取策略,batch-szie在集合上的应用

batch-size属性,能够批量载入实体类,參见:Classes.hbm.xml
<set name="students" inverse="true" cascade="all" batch-size="5">

当查students对象时发出10条hql语句配置过后batch-size=5后会之发10/5=2条hql语句,提高性能

 

 

整体上分析:默认是fetch="select"       当配置fetch="join"时直接查询包括的对象或者集合lazy失效。

 

转载地址:http://wnrhx.baihongyu.com/

你可能感兴趣的文章
常用纹理数据库
查看>>
1999 - 2009的集训队论文
查看>>
java 检测代理IP是否准确
查看>>
Es学习第十课,ElasticSearch集群搭建
查看>>
centos 7 部署 open-falcon 0.2.0
查看>>
如何利用反射简化Servlet操作
查看>>
Java语言实现简单FTP软件------>FTP软件主界面的实现(四)
查看>>
Virtualbox报错------> VirtualBox虚拟机下鼠标不正常的解决方法
查看>>
Java 中时间处理SimpleDateFormat 中HH和hh的区别
查看>>
java中Collections.sort排序详解
查看>>
C语言基础学习day08
查看>>
《世界是数字的》读后感
查看>>
点云平台之图像算法篇
查看>>
读取txt/csv/xml文件
查看>>
Java中String、StringBuffer和StringBuilder的区别
查看>>
FFmpeg数据结构AVBuffer
查看>>
Algs4-1.1.16给出exR1(6)的返回值
查看>>
Vuex
查看>>
swiper的理解
查看>>
04-1下载Win系统(装机助理)
查看>>