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",如:
<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失败。

时间: 2024-10-06 23:43:32

Hibernate的fetch的相关文章

hibernate之fetch

处理关联关系是ORM中一常见操作,特别是在查询的时候,经常要在查询某个实体的时候要把它实体关联属性也查询出来,例如查询用户时级联查询角色信息,还有可能角色及联查询权限信息.在hibernate中实现这个目的有很多总方式: 1.配置OpenSessionInViewFilter,让Session在View层中保存打开状态,可以随时使用,这看起来是个一劳永逸的办法,但其也带来了一些问题,  至于会有什么样的问题百度会给你答案. 2.在映射实体时把关联属性设置lazy="false",表示该

Hibernate的fetch (转)

fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例: fetch = "select"是在查询的时候先查询出一端的实体,然后在根据一端的查询出多端的实体,会产生1+n条sql语句; fetch = "join"是在查询的时候使用外连接进行查询,不会差生1+n的现象. lazy大家应该都熟悉了,就是延迟加载,可以设置lazy = "true" , lazy

Hibernate中fetch和lazy介绍

fetch ,指定关联对象抓取的方式,可以设置fetch = "select" 和 fetch = "join".select方式时先查询返回要查询的主体对象(列表),再根据关联外键id,每一个对象发一个select查询,获取关联的对象,形成n+1次查询:而join方式,主体对象和关联对象用一句外键关联的sql同时查询出来,不会形成多次查询. 在映射文档中定义的抓取策略将会有产生以下影响: (1)通过get()或load()方法取得数据. (2)只有在关联之间进行导

Hibernate - lazy, fetch, inverse, cascade

Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了"inverse=false"(默认),那么那一方就有责任负责之间的关联关系,说白了就是hibernate如何生成Sql来维护关联的记录! inverse=true    ->    由另一方维护关联关系inverse=false    ->    由当前方维护关联关系 父亲中的关系映射   {set name="children&q

hibernate中fetch lazy

join 查询的时候,是用一条语句查处所有记录,包括关联表记录, select查出的是N+1条记录,两个都是差不多的,但是如果用了lazy=true,延迟加 载的话,select在查询时只会查出主表记录,也就是1,如果其他地方也用到了数据,此时就会自动在执行查询,查出N,可以降低内存消耗 .还有,hibernate是的session是轻量级的,创建和销毁都不花很多资源,查询数据也很快,这里fetch主要起这个作用,所以我们一般的情 况下要用select查询 fetch="join",已

Hibernate中得fetch

fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例:fetch = "select"是在查询的时候先查询出一端的实体,然后在根据一端的查询出多端的实体,会产生1+n条sql语句;fetch = "join"是在查询的时候使用外连接进行查询,不会差生1+n的现象. lazy大家应该都熟悉了,就是延迟加载,可以设置lazy = "true" , lazy =

11.hibernate的连接查询

1.创建如下javaweb项目结构 2.在项目的src下创建hibernate.cfg.xml主配置文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourc

hibernate的CRUD操作

本文简单总结一下hibernate的具有映射关系CRUD操作,主要使用到了cascade和fetch,其中cascade针对的是CUD操作,表示级联关系,fetch针对的是R操作,表示级联加载方式. 1.先看下javaEE的API中关于cascade的描述 javax.persistence Enum CascadeType java.lang.Object java.lang.Enum<CascadeType> javax.persistence.CascadeType All Implem

HQL: The Hibernate Query Language

Chapter 14. HQL: The Hibernate Query Language 14.1. Case Sensitivity 14.2. The from clause 14.3. Associations and joins 14.4. Forms of join syntax 14.5. Referring to identifier property 14.6. The select clause 14.7. Aggregate functions 14.8. Polymorp