检索策略

  类级别检索

    默认检索策略:默认延迟加载, 可以使用lazy属性来进行改变.        

  session.get(clazz,object)默认立即加载

    @Test	//测试左外连接查询
public void test13(){
Session session = HibernateUtils.getSession();
Transaction bt = session.beginTransaction();
Customer customer = session.get(Customer.class,1);
bt.commit();
session.close();
}

  

  session.load(clazz,object)默认延迟加载    可以使用Hibernate.initialize(customer)初始化数据;

      @Test	//load延迟加载
public void test14(){
Session session = HibernateUtils.getSession();
Transaction bt = session.beginTransaction();
Customer customer = session.load(Customer.class,1);
bt.commit();
session.close();
}

关联级别检索

  一对一<one-to-one>

  一对多/多对一   <set>下有<one-to-many>      <many-to-one>

  多对多<many-to-many>

我们主要是在set下one-to-many或many-to-one设置lazy和fetch

查询一个客户下的订单

    set上

lazy
true 延迟检索
false 立即检索
extra 加强延迟加载
fetch
select 多条简单的sql语句
join 采用迫切左外连接
subselect 将生成子查询的sql

未进行设置则默认为懒加载

@Test
public void test15(){
Session session = HibernateUtils.getSession();
Transaction bt = session.beginTransaction();
String hql="from Customer c right outer join c.orders";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
bt.commit();
session.close();
}

 sql打印:

Hibernate:
select
customer0_.c_id as c_id1_0_,
customer0_.name as name2_0_
from
test.c_customer customer0_

  打印结果证明只进行查询了customer对象

  其中Order对象未进行加载  这样就是在调用时会发送sql语句进行查询  为了解决这一事件  我们让Customer立即加载

修改其配置文件:

  <set lazy="false" > //设置立即加载

  再次执行会sql打印

Hibernate:
select
customer0_.c_id as c_id1_0_,
customer0_.name as name2_0_
from
test.c_customer customer0_
Hibernate:
select
orders0_.o_customer_id as o_custom4_1_0_,
orders0_.o_id as o_id1_1_0_,
orders0_.o_id as o_id1_1_1_,
orders0_.o_money as o_money2_1_1_,
orders0_.o_receiverInfo as o_receiv3_1_1_,
orders0_.o_customer_id as o_custom4_1_1_
from
test.o_order orders0_
where
orders0_.o_customer_id=?
Hibernate:
select
orders0_.o_customer_id as o_custom4_1_0_,
orders0_.o_id as o_id1_1_0_,
orders0_.o_id as o_id1_1_1_,
orders0_.o_money as o_money2_1_1_,
orders0_.o_receiverInfo as o_receiv3_1_1_,
orders0_.o_customer_id as o_custom4_1_1_
from
test.o_order orders0_
where
orders0_.o_customer_id=?

  这样在查询customer时会进行查询order

测试lazy=extra属性

<set lazy="true" > //设置延迟加载

  执行以下方法

      @Test
public void test15(){
Session session = HibernateUtils.getSession();
Transaction bt = session.beginTransaction();
String hql="from Customer";
Query query = session.createQuery(hql);
List<Customer> list = query.list();
/*int size = list.size();
System.out.println(size);*/
for (Customer customer : list) {
System.out.println(customer.getOrders().size());
}
//操作
bt.commit();
session.close();
}

  sql打印:

Hibernate:
select
customer0_.c_id as c_id1_0_,
customer0_.name as name2_0_
from
test.c_customer customer0_
Hibernate:
select
orders0_.o_customer_id as o_custom4_1_0_,
orders0_.o_id as o_id1_1_0_,
orders0_.o_id as o_id1_1_1_,
orders0_.o_money as o_money2_1_1_,
orders0_.o_receiverInfo as o_receiv3_1_1_,
orders0_.o_customer_id as o_custom4_1_1_
from
test.o_order orders0_
where
orders0_.o_customer_id=?
1
Hibernate:
select
orders0_.o_customer_id as o_custom4_1_0_,
orders0_.o_id as o_id1_1_0_,
orders0_.o_id as o_id1_1_1_,
orders0_.o_money as o_money2_1_1_,
orders0_.o_receiverInfo as o_receiv3_1_1_,
orders0_.o_customer_id as o_custom4_1_1_
from
test.o_order orders0_
where
orders0_.o_customer_id=?
100

  

<set lazy="extra" > //设置加强延迟加载

  sql打印为:

Hibernate:
select
customer0_.c_id as c_id1_0_,
customer0_.name as name2_0_
from
test.c_customer customer0_
Hibernate:
select
count(o_id)
from
test.o_order
where
o_customer_id =?
1
Hibernate:
select
count(o_id)
from
test.o_order
where
o_customer_id =?
100

  

总结:
对于懒加载和加强懒加载区别:都是是在调用时才会产生,但是区别在于发送sql语句的意义;
懒加载在发送sql语句时会发送查询全部的语句,返回为每一列,而加强懒加载发送的是count(*),我需要什么加强懒加载会给我什么直接查询,而懒加载不会.

  

测试fetch    只是sql的生成方式不同而已

 <set name="orders"  lazy="false" fetch="subselect" >  //subselect生成子查询
     @Test
public void test15(){
Session session = HibernateUtils.getSession();
Transaction bt = session.beginTransaction();
String hql="from Customer";
Query query = session.createQuery(hql);
List<Customer> list = query.list();
bt.commit();
session.close();
}

  sql打印:

Hibernate:
select
customer0_.c_id as c_id1_0_,
customer0_.name as name2_0_
from
test.c_customer customer0_
Hibernate:
select
orders0_.o_customer_id as o_custom4_1_1_,
orders0_.o_id as o_id1_1_1_,
orders0_.o_id as o_id1_1_0_,
orders0_.o_money as o_money2_1_0_,
orders0_.o_receiverInfo as o_receiv3_1_0_,
orders0_.o_customer_id as o_custom4_1_0_
from
test.o_order orders0_
where
orders0_.o_customer_id in (    //子查询
select
customer0_.c_id
from
test.c_customer customer0_
)

  

                                  在<many-to-one>或<one-to-one>如果去查询对方

fetch
select  
join  
lazy
false 立即加载
proxy 是否采用延迟,由另一方决定
no-proxy 不做研究

hibernate检索策略(抓取策略)的更多相关文章

  1. Hibernate(十四)抓取策略

    抓取策略: 抓取策略是当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.Hibernate的抓取策略是Hibernate提升性能的一 ...

  2. hibernate 延迟加载和抓取策略

    一.延迟加载 1.简单查询get,load 针对对象本身延迟或即时 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load ...

  3. Hibernate学习---第十一节:Hibernate之数据抓取策略&批量抓取

    1.hibernate 也可以通过标准的 SQL 进行查询 (1).将SQL查询写在 java 代码中 /** * 查询所有 */ @Test public void testQuery(){ // ...

  4. 【转】hibernate延迟加载和抓取策略

    一.延迟加载 1.简单查询get,load 针对对象本身延迟或即时 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load ...

  5. 【Hibernate学习】 —— 抓取策略(注解方式)

    当应用程序须要在关联关系间进行导航的时候.hibernate怎样获取关联对象的策略. 抓取策略的方式: FetchType.LAZY:懒载入.载入一个实体时.定义懒载入的属性不会立即从数据库中载入. ...

  6. Hibernate查询方式&抓取策略

    Hibernate的查询方式 1.OID查询 hibernate根据对象的OID(主键)进行检索 使用get方法 Customer customer=session.get(Customer.clas ...

  7. 【Hibernate 8】Hibernate的调优方法:抓取策略

    在上一篇博客中,介绍了Hibernate的缓存机制.合理的配置缓存,可以极大程度上优化Hibernate的性能.这篇博客,介绍另外一个调优方式:抓取策略. 一.什么是抓取策略 抓取策略(fetchin ...

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

    1.Hibernate中的多表查询 1.1SQL中的多表查询 [交叉连接] select * from A,B; [内连接] 显示内连接:inner join(inner 可以省略) Select * ...

  9. Hibernate的抓取策略(优化)

    延迟加载的概述 什么是延迟加载 延迟加载:lazy(懒加载).执行到该行代码的时候,不会发送语句去进行查询,在真正使用这个对象的属性的时候才会发送SQL语句进行查询. 延迟加载的分类 l  类级别的延 ...

随机推荐

  1. VsCode基本使用

    迫于公司统一编辑器,初次接触VsCode,小白入门笔记 安装插件及其用途: 1. Bracket Pair Colorizer :对括号对进行着色,再也不会搞不清状况了. 2. Git History ...

  2. Python中setup.py一些不为人知的技巧

    http://python.jobbole.com/80912/ 在我开始之前,我想先说清楚我将要解释的是些“窍门”.他们不是“最好的做法”,至少在一种情况下是不可取的. 说到不可取的做法,我会适时写 ...

  3. Numpy copy & deep copy

    1. '='的赋值方式会带有关联性 >>> import numpy as np >>> a = np.arange(4) >>> b = a & ...

  4. 最大子序列(java版)

    package com.algorithm.test; /** * 最大子序列 * @author LiFen * */ public class LargestSubsequence { publi ...

  5. egg 为企业级框架和应用而生, 阿里出品

    https://eggjs.org/zh-cn/intro/ egg 是什么? egg 为企业级框架和应用而生,我们希望由 egg 孕育出更多上层框架,帮助开发团队和开发人员降低开发和维护成本. 设计 ...

  6. A面&B面

    难难难.道是玄,不遇知音不可谈.遇了知音聊两句,免教那枉费舌尖.难得今天心情不错,反思毕业这五年的种种,有浑噩.迷茫.彷徨.莽撞.执着.困顿.不惧,走到今天迈过了几道坎早已忘却,同时也还在询问自己值不 ...

  7. Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils

    Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...

  8. c++11 template 模板练习

    直接上代码吧 to do // 111111.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...

  9. robot framework 中should be true 与should contain 的区别

    should be true  是否等于:判断是否should contain  是否包含 a是否包含b

  10. 2017年7月25日多校一Function

    Function这道题我当时一直很迷,到底怎么来的啊,为什么会这样啊?? 然后看了题解才知道,原来是找循环啊. 已知f(i)=b[f(a(i)],则 f(0) = b[f(a[0])] = b[f(2 ...