Hibernate的fetch
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",如: fetch="select",另外发送一条select语句抓取当前对象关联实体或集合 2.hibernate抓取策略(单端代理的批量抓取) 设置fetch="join",如: fetch="join",hibernate会通过select语句使用外连接来载入其关联实体或集合 此时lazy会失效 3.hibernate抓取策略(集合代理的批量抓取) 保持默认,同fetch="select",如: fetch="select",另外发送一条select语句抓取当前对象关联实体或集合 4.hibernate抓取策略(集合代理的批量抓取) 设置fetch="join",如: fetch="join",hibernate会通过select语句使用外连接来载入其关联实体或集合 此时lazy会失效 5.hibernate抓取策略(集合代理的批量抓取) 设置fetch="subselect",如: fetch="subselect",另外发送一条select语句抓取在前面查询到的全部实体对象的关联集合 6.hibernate抓取策略,,batch-szie在<class>上的应用 batch-size属性,能够批量载入实体类,參见:Classes.hbm.xml 当查classes对象时发出9条hql语句配置过后batch-size=3后会之发9/3=3条hql语句,提高性能 7.hibernate抓取策略,batch-szie在集合上的应用 batch-size属性,能够批量载入实体类,參见:Classes.hbm.xml 当查students对象时发出10条hql语句配置过后batch-size=5后会之发10/5=2条hql语句,提高性能 整体上分析:默认是fetch="select" |
Hibernate的fetch的更多相关文章
- Hibernate的fetch (转)
fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例: fetch = "select" ...
- FW: How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot + MySQL
原帖 https://grokonez.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql In the ...
- Hibernate - lazy, fetch, inverse, cascade
Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了"inverse=false"(默认),那么那一 ...
- hibernate中fetch lazy
join 查询的时候,是用一条语句查处所有记录,包括关联表记录, select查出的是N+1条记录,两个都是差不多的,但是如果用了lazy=true,延迟加 载的话,select在查询时只会查出主表记 ...
- Hibernate中fetch和lazy介绍
fetch ,指定关联对象抓取的方式,可以设置fetch = "select" 和 fetch = "join".select方式时先查询返回要查询的主体对象( ...
- hibernate 中 fetch=FetchType.LAZY 懒加载失败处理
对这种懒加载问题,最后的做法是利用Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,使得Hi ...
- Hibernate中得fetch
fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例:fetch = "select"是 ...
- 11.hibernate的连接查询
1.创建如下javaweb项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 <?xml version="1.0" encoding=" ...
- HQL: The Hibernate Query Language
Chapter 14. HQL: The Hibernate Query Language 14.1. Case Sensitivity 14.2. The from clause 14.3. Ass ...
随机推荐
- FFMPEG 截取RTMP直播流图片命令
CentOS 6.5 yum安装FFMPEG步骤 1. 手动添加yum源配置 vi /etc/yum.repos.d/dag.repo [dag] name=Dag RPM Repository ...
- Android学习----ADB
adb是什么?:adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具.a ...
- bootstrap导航条
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>我的 ...
- 利用Warensoft Stock Service编写高频交易软件
利用Warensoft Stock Service编写高频交易软件 无论是哪种交易软件,对于程序员来讲,最麻烦的就是去实现各种算法.本文以SAR算法的实现过程为例,为大家说明如何使用Warensoft ...
- MYSQL管理之主从同步管理 转载
MYSQL主从同步架构是目前使用最多的数据库架构之一,尤其是负载比较大的网站,因此对于主从同步的管理也就显得非常重要,新手往往在出现主从同步错误的时候不知道如何入手,这篇文章就是根据自己的经验来详细叙 ...
- 由于权限不足而无法读取配置文件出现的HTTP 500.19解决办法
无法访问请求的页面,因为该页的相关配置数据无效. 如下图: 解决方法, 到站点目录的属性,安全标签,添加用户(Everyone),并给修改权限:
- python抓取网页图片
本人比较喜欢海贼王漫画,所以特意选择了网站http://www.mmonly.cc/ktmh/hzw/list_34_2.html来抓取海贼王的图片. 因为是刚刚学习python,代码写的不好,不要喷 ...
- powerpoint2015如何调整PPT立体图形
在powerpoint2016中,可以添加图片来丰富演说,但通常人们只会懂得插入平面图片.动态图片,立体图片就无法插入了吗?不是的,在powerpoint2016为我们准备了大量的预设,可以调整图片, ...
- CodeForces 569A 第六周比赛C踢
C - C Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 大数据学习之测试hdfs和mapreduce(二)
上篇已经搭建好环境,本篇主要测试hadoop中的hdfs和mapreduce功能. 首先填坑:启动环境时发现DataNode启动不了.查看日志 从日志中可以看出,原因是因为datanode的clust ...