`

hibernate抓取策略fetch=select /join/subselect

 
阅读更多

出处:http://blog.csdn.net/ychatou1220/article/details/6489881

一、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语句抓取当前对象关联实体或集合

执行结果: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语句使用外连接来加载其关联实体或集合

此时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)保持默认,同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))
9
student.name=学生8
student.name=学生4
student.name=学生5
student.name=学生9
student.name=学生6
student.name=学生2
student.name=学生3
student.name=学生1
student.name=学生7
Class.name=高一(2)班
4
student.name=学生3
student.name=学生4
student.name=学生2
student.name=学生1
Class.name=高一(3)班
0

分享到:
评论

相关推荐

    HIBERNATE检索策略

    默认抓取策略(Fetch="select") 这是HIBERNATE默认的抓取策略,当一个实体被加载时,与之关联的实体或集合并不会立即加载,而是采用延迟加载(Lazy Loading)的方式。当第一次访问关联实体或集合时,HIBERNATE会...

    Hibernate中的多表查询及抓取策略

    - **抓取策略**:通过设置`fetch`属性,可以控制关联对象的加载方式: - `select`:默认值,使用单独的SELECT语句获取关联对象。 - `join`:使用迫切左外连接(Eager Fetching),一次性加载关联对象。 - `...

    Hibernate延迟加载原理与实现方法

    - 集合属性上的抓取策略类似,`fetch="join"`会导致关联集合的延迟加载失效,而`fetch="subselect"`会使用子查询来获取关联数据。 4. 案例分析: 在一个测试类中,通过`Session.load()`方法加载用户实体,如果未...

    如何提高hibernate性能

    当业务层需要为Web层提供完整的数据集合时,确保在Session关闭前调用`Hibernate.initialize()`来初始化集合,或者在查询中使用`FETCH`从句或`FetchMode.JOIN`以预加载所需的数据。这避免了因延迟加载导致的多次...

    Hibernate+中文文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    hibernate3.2中文文档(chm格式)

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    HibernateAPI中文版.chm

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    最全Hibernate 参考文档

    19.1.1. 调整抓取策略(Tuning fetch strategies) 19.1.2. 单端关联代理(Single-ended association proxies) 19.1.3. 实例化集合和代理(Initializing collections and proxies) 19.1.4. 使用批量抓取(Using...

    Hibernate中文详细学习文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    Hibernate 中文 html 帮助文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    Hibernate教程

    20.1.2. 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) 20.1.4. 实例化集合和代理(Initializing collections and proxies) 20.1.5. 使用批量抓取(Using...

    hibernate 体系结构与配置 参考文档(html)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    Hibernate3+中文参考文档

    19.1.1. 调整抓取策略(Tuning fetch strategies) 19.1.2. 单端关联代理(Single-ended association proxies) 19.1.3. 实例化集合和代理(Initializing collections and proxies) 19.1.4. 使用批量抓取(Using...

    hibernate3.04中文文档.chm

    20.1.2. 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) 20.1.4. 实例化集合和代理(Initializing collections and proxies) 20.1.5. 使用批量抓取...

    hibernate 框架详解

    目录 前言 1.... 2.... 1. 在Tomcat中快速上手 ... 1.1. 开始Hibernate之旅 1.2.... 1.3.... 1.4.... 调整抓取策略(Tuning fetch strategies) 20.1.3. 单端关联代理(Single-ended association proxies) ...

    Hibernate参考文档

    19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using...

    NHibernate参考文档 2.0.0 chm

    16.1.2. 调整抓取策略(Tuning fetch strategies) 16.1.3. 单端关联代理(Single-ended association proxies) 16.1.4. 初始化集合和代理(Initializing collections and proxies) 16.1.5. 使用批量抓取(Using ...

    NHibernate中文帮组文档(2008.11月更新)

    16.1.2. 调整抓取策略(Tuning fetch strategies) 16.1.3. 单端关联代理(Single-ended association proxies) 16.1.4. 初始化集合和代理(Initializing collections and proxies) 16.1.5. 使用批量抓取(Using ...

Global site tag (gtag.js) - Google Analytics