Hibernate的fetch

时间:2022-12-21 16:04:05

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失效。

Hibernate的fetch的更多相关文章

  1. Hibernate的fetch (转)

    fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例: fetch = "select" ...

  2. FW&colon; How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot &plus; MySQL

    原帖 https://grokonez.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql In the ...

  3. Hibernate - lazy&comma; fetch&comma; inverse&comma; cascade

    Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了"inverse=false"(默认),那么那一 ...

  4. hibernate中fetch lazy

    join 查询的时候,是用一条语句查处所有记录,包括关联表记录, select查出的是N+1条记录,两个都是差不多的,但是如果用了lazy=true,延迟加 载的话,select在查询时只会查出主表记 ...

  5. Hibernate中fetch和lazy介绍

    fetch ,指定关联对象抓取的方式,可以设置fetch = "select" 和 fetch = "join".select方式时先查询返回要查询的主体对象( ...

  6. hibernate 中 fetch&equals;FetchType&period;LAZY 懒加载失败处理

    对这种懒加载问题,最后的做法是利用Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,使得Hi ...

  7. Hibernate中得fetch

    fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例:fetch = "select"是 ...

  8. 11&period;hibernate的连接查询

    1.创建如下javaweb项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 <?xml version="1.0" encoding=&quot ...

  9. HQL&colon; The Hibernate Query Language

    Chapter 14. HQL: The Hibernate Query Language 14.1. Case Sensitivity 14.2. The from clause 14.3. Ass ...

随机推荐

  1. 添加ssh key

    我现在根据<github入门和实践>来去摸索github 其实,我发现自己在看github时,感觉不适应,是因为自己太久没有碰到英文了.可以联想到以前当看到一个网页,根据汉字的标题或描述, ...

  2. PostgreSQL入门

    这篇文章主要介绍我在最近工作中接触到的数据库PostgreSQL,做个入门记录,以便自己日后回顾. 1.下载安装,安装配置过程可以参考http://blog.chinaunix.net/uid-354 ...

  3. 关于实现自定义Dialog和实现Dialog里view的事件监听的两种方法

    一.自定义dialog. 二.实现dialog里view的事件监听 1.自定义dialog比较简单.在实例化new的时候,加入样式,布局就行了.或者重写dialog. 2.实现dialog里view的 ...

  4. CodeForces 534B Covered Path &lpar;水题&rpar;

    题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...

  5. ThinkPHP3&period;2 加载过程(二)

    回顾: 上次介绍了 ThinkPHP 的 Index.PHP入口文件.但只是TP的入口前面的入口(刷boss总是要过好几关才能让你看到 ,不然boss都没面子啊),从Index.PHP最后一行把我们引 ...

  6. Java compile时,提示 DeadCode的原因

    在工程编译时,编译器发现部分代码是无用代码,则会提示:某一行代码是DeadCode.今天compile工程的时候发现某一个循环出现这个问题,如下: public void mouseOver(fina ...

  7. 做 Excel 的 XML schema&period;xsd

    做 Excel 的 XML schema.xsd 因为需要将 BOM Cost 归档并使用 Git 管理,原来 XLSX 是二进制的,使用 Git 管理并不方便. 于是有了想用 XML 来管理想法,在 ...

  8. C&plus;&plus;中成员变量默认private

    struct 默认是 publicclass 默认是 private

  9. virtualbox测试k8s要注意的情况

    想在virtualBox上测试k8s,遇到两个情况要注意.. 第一是flannel和dashborad起不起来,master都无法正常..这时可以想办法把Iptables,selinux,firewa ...

  10. Iptables详解&plus;实例

    Iptabels是与Linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的功能.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理 ...