检索策略

  类级别检索

    默认检索策略:默认延迟加载, 可以使用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. 计数器counter

    今天就讲了2个属性:1.计数器 2.列规则 列规则很简单:column-count:3; (列的具体个数) column-width:30px;(列宽)N个浏览器不兼容column-gap:10px; ...

  2. Hadoop集群配置(最全面总结 )(转)

    Hadoop集群配置(最全面总结) huangguisu 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为Da ...

  3. Centos7 上安装 FastDFS

    1.安装gcc(编译时需要) FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc yum install -y gcc ...

  4. redis的五种存储类型的具体用法

    String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 $redis-> ...

  5. Tinyos学习笔记(二)

    1.TinyOS communication tools java serialApp -comm serial@/dev/ttyUSB0:telosb java net.tinyos.tools.L ...

  6. android 打开新窗口

    ImageView loginBtn = (ImageView)findViewById(R.id.login_button); loginBtn.setOnClickListener(new Vie ...

  7. 【搜索】 Prime Path

    #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include& ...

  8. 新版的Bing Developer Assistant 已发布,去掉了Beta

    网址:https://visualstudiogallery.msdn.microsoft.com/a1166718-a2d9-4a48-a5fd-504ff4ad1b65 新加特性: New Vis ...

  9. TryXXX模式(深入理解c#)

    .NET有几个模式很容易根据所涉及的方法名称来识别.例如,BeginXXX和EndXXX暗示着一个异步操作.TryXXX模式的用途在.net1.1升级到2.0期间进行了扩展.他是针对以下情况设计的:有 ...

  10. [RequireComponent(typeof(....))]

    RequireComponent的使用: 当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体).这个可以有效的避免组装错误.举个例 ...