一、Hibernate 的检索策略本质上是为了优化 Hibernate 性能。

二、Hibernate 检索策略包括类级别的检索策略、和关联级别的检索策略(<set> 元素)

三、类级别的检索策略

1. 立即检索、延迟检索

2. 通过 <class> 节点的 lazy 属性来控制。默认为 true,即为延迟检索。

3. 只针对 session 的 load() 方法生效。

默认情况下,通过 load() 方法获取到的对象是一个代理对象,Hibernate 创建代理对象时,仅会初始化 OID。

在第一次访问非 OID 属性时,Hibernate 会初始化代理类实例。

4.测试

(1)<class> 的 lazy 属性为 true

@Test
public void testStrategyClassLevel() {
Customer customer = (Customer) session.load(Customer.class, 5);
System.out.println(customer.getClass());
System.out.println(customer.getCustomerId());
System.out.println("--------------");
System.out.println(customer.getCustomerName());
}
class com.nucsoft.strategy.many2one.Customer_$$_javassist_0
5
--------------
Hibernate:
select
customer0_.customer_id as customer1_2_0_,
customer0_.customer_name as customer2_2_0_
from
hibernate.customer customer0_
where
customer0_.customer_id=?
bb

(2)<class> 的 lazy 属性为 false

@Test
public void testStrategyClassLevel() {
session.load(Customer.class, 5);
}
Hibernate:
select
customer0_.customer_id as customer1_2_0_,
customer0_.customer_name as customer2_2_0_
from
hibernate.customer customer0_
where
customer0_.customer_id=?

四、关联级别的检索策略

1. 指的是用 <set> 元素来配置的多对一和多对多的关联关系。

2. 主要指的是 <set> 元素的三个属性:lazy 、fetch、batch-size。

3. lazy 属性(默认为 true,即采用延迟检索策略)

(1)决定集合被初始化的时机。

(2)取值为 true 时

  • Hibernate 会在以下情况下初始化集合代理类实例

    • 访问集合属性、iterator()、size()、isEmpty()、contains() 等方法时
    • 通过 Hibernate.initialize() 静态方法显示的初始化  

(3)取值为 false 时

(4)取值为 extra 时(增强的延迟检索策略),会尽可能的延迟初始化集合的时机。如:

@Test
public void testSetLazy() {
Category category = (Category) session.get(Category.class, 5);
System.out.println(category.getItems().size());
}
Hibernate:
select
category0_.category_id as category1_1_0_,
category0_.category_name as category2_1_0_
from
hibernate.category category0_
where
category0_.category_id=?
Hibernate:
select
count(item_id)
from
hibernate.categories_items
where
category_id =?
2

调用集合的 size() 方法时,是通过 count() 来查询的。

当调用集合的 iterator() 方法时,会初始化集合。

4. fetch 属性(默认为 "select")

(1)取值为 "select" 或 "subselect" 时,决定初始化集合查询语句的形式。

(2)取值为 "join",则决定初始化集合的时机。会忽略 "lazy" 属性。

(3)测试

<1>取值为 "select"

@Test
public void testSetLazy() {
List<Category> categories = session.createQuery("from Category").list(); for(Category category : categories) {
System.out.println(category.getItems().size());
} }
Hibernate:
select
customer0_.customer_id as customer1_2_,
customer0_.customer_name as customer2_2_
from
hibernate.customer customer0_
Hibernate:
select
orders0_.customer_id as customer3_2_1_,
orders0_.order_id as order1_4_1_,
orders0_.order_id as order1_4_0_,
orders0_.order_name as order2_4_0_,
orders0_.customer_id as customer3_4_0_
from
hibernate.order orders0_
where
orders0_.customer_id=?
2
Hibernate:
select
orders0_.customer_id as customer3_2_1_,
orders0_.order_id as order1_4_1_,
orders0_.order_id as order1_4_0_,
orders0_.order_name as order2_4_0_,
orders0_.customer_id as customer3_4_0_
from
hibernate.order orders0_
where
orders0_.customer_id=?
2
Hibernate:
select
orders0_.customer_id as customer3_2_1_,
orders0_.order_id as order1_4_1_,
orders0_.order_id as order1_4_0_,
orders0_.order_name as order2_4_0_,
orders0_.customer_id as customer3_4_0_
from
hibernate.order orders0_
where
orders0_.customer_id=?
2
Hibernate:
select
orders0_.customer_id as customer3_2_1_,
orders0_.order_id as order1_4_1_,
orders0_.order_id as order1_4_0_,
orders0_.order_name as order2_4_0_,
orders0_.customer_id as customer3_4_0_
from
hibernate.order orders0_
where
orders0_.customer_id=?
2

<2>取值为 "subselect",会忽略 batch-size 属性。

Hibernate:
select
category0_.category_id as category1_1_,
category0_.category_name as category2_1_
from
hibernate.category category0_
Hibernate:
select
items0_.category_id as category1_1_1_,
items0_.item_id as item2_0_1_,
item1_.item_id as item1_3_0_,
item1_.item_name as item2_3_0_
from
hibernate.categories_items items0_
inner join
hibernate.item item1_
on items0_.item_id=item1_.item_id
where
items0_.category_id in (
?, ?
)
2
2
Hibernate:
select
customer0_.customer_id as customer1_2_,
customer0_.customer_name as customer2_2_
from
hibernate.customer customer0_
Hibernate:
select
orders0_.customer_id as customer3_2_1_,
orders0_.order_id as order1_4_1_,
orders0_.order_id as order1_4_0_,
orders0_.order_name as order2_4_0_,
orders0_.customer_id as customer3_4_0_
from
hibernate.order orders0_
where
orders0_.customer_id in (
select
customer0_.customer_id
from
hibernate.customer customer0_
)
2
2
2
2

通过子查询的方式,通过 in 的方式。

<3>取值为 "join"

  • 会采用迫切左外链接(使用左外链接进行查询,同时初始化集合属性)策略来初始化所有关联的对象
  • lazy 属性将会被忽略
  • Query 的 list() 方法会忽略这个取值,不会忽略 lazy 属性。
  • HQL 会忽略取值为 "join" 的取值
@Test
public void testFetch() {
session.get(Customer.class, 5);
}
Hibernate:
select
customer0_.customer_id as customer1_2_1_,
customer0_.customer_name as customer2_2_1_,
orders1_.customer_id as customer3_2_3_,
orders1_.order_id as order1_4_3_,
orders1_.order_id as order1_4_0_,
orders1_.order_name as order2_4_0_,
orders1_.customer_id as customer3_4_0_
from
hibernate.customer customer0_
left outer join
hibernate.order orders1_
on customer0_.customer_id=orders1_.customer_id
where
customer0_.customer_id=?

5.batch-size 属性,设定批量检索集合的数量。

(1)默认情况下

@Test
public void testSetLazy() {
List<Category> categories = session.createQuery("from Category").list(); for(Category category : categories) {
System.out.println(category.getItems().size());
} }
Hibernate:
select
category0_.category_id as category1_1_,
category0_.category_name as category2_1_
from
hibernate.category category0_
Hibernate:
select
items0_.category_id as category1_1_1_,
items0_.item_id as item2_0_1_,
item1_.item_id as item1_3_0_,
item1_.item_name as item2_3_0_
from
hibernate.categories_items items0_
inner join
hibernate.item item1_
on items0_.item_id=item1_.item_id
where
items0_.category_id=?
2
Hibernate:
select
items0_.category_id as category1_1_1_,
items0_.item_id as item2_0_1_,
item1_.item_id as item1_3_0_,
item1_.item_name as item2_3_0_
from
hibernate.categories_items items0_
inner join
hibernate.item item1_
on items0_.item_id=item1_.item_id
where
items0_.category_id=?
2

(2)设置 batch-size="2"

@Test
public void testSetLazy() {
List<Category> categories = session.createQuery("from Category").list(); for(Category category : categories) {
System.out.println(category.getItems().size());
} }
Hibernate:
select
category0_.category_id as category1_1_,
category0_.category_name as category2_1_
from
hibernate.category category0_
Hibernate:
select
items0_.category_id as category1_1_1_,
items0_.item_id as item2_0_1_,
item1_.item_id as item1_3_0_,
item1_.item_name as item2_3_0_
from
hibernate.categories_items items0_
inner join
hibernate.item item1_
on items0_.item_id=item1_.item_id
where
items0_.category_id in (
?, ?
)
2
2

五、<many-to-one> 元素的 lazy 和 fetch 属性

1.lazy 属性

(1)proxy

采用延迟检索策略。

(2)false

采用立即检索策略。

2.fetch 属性

(1)select(默认)

(2)join

表示使用迫切左外链接的方式,初始化 n 的一端关联的 1 的一端的对象。

同样,HQL 查询会忽略 fetch 的取值为 join 的情况,此种情况下,若想批量初始化 1 的一端的代理对象,可以在 1 的一端的 class 节点添加 batch-size 属性。

六、总结

介绍了 Hibernate 的检索策略,包括累级别的检索策略,和关联级别的检索策略。以及简单介绍了 <many-to-one> 元素的 lazy 和 fetch 属性。

Hibernate —— 检索策略的更多相关文章

  1. hibernate(八) Hibernate检索策略(类级别,关联级别,批量检索)详解

    序言 很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要 ...

  2. Hibernate 检索策略

    概述 检索数据时的 2 个问题: –不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象, 而程序实际上仅仅需要访问 Custome ...

  3. Hibernate检索策略(抓取策略)(Hibernate检索优化)

    一.查询方法中get方法采用策略是立即检索,而load方法采用策略是延迟检索,延迟检索是在使用数据时才发送SQL语句加载数据 获取延迟加载数据方式:1.使用的时候,如果Customer c=sessi ...

  4. [原创]java WEB学习笔记88:Hibernate学习之路-- -Hibernate检索策略(立即检索,延迟检索,迫切左外连接检索)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Hibernate检索策略

    1. Hibernate的检索策略概述: 检索数据时的 2 个问题:    1.不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象 ...

  6. (九)Hibernate 检索策略

    所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 这里的hibernate.cfg.xml配置信息我就不再写了 第一节:检 ...

  7. Hibernate学习(八)———— Hibernate检索策略(类级别,关联级别,批量检索)详解

    序言 很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要 ...

  8. hibernate检索策略(抓取策略)

    检索策略 类级别检索 默认检索策略:默认延迟加载, 可以使用lazy属性来进行改变. session.get(clazz,object)默认立即加载 @Test //测试左外连接查询 public v ...

  9. Hibernate检索策略与检索方式

    hibernate的Session在加载Java对象时,一般都会把鱼这个对象相关联的其他Java对象也都加载到缓存中,以方便程序的调用.但很多情况下,我们不需要加载太多无用的对象到缓存中,一来会占用大 ...

随机推荐

  1. Linux搜索文件夹下所有文件中字符串

    1.grep "status_bar_height" * -nR /* 搜索文件中有很多不存在的文件出现 */ 2.grep -nsr "status_bar_heigh ...

  2. Android WiFi密码(查看工具)

    纯手机端AIDE编写,现在分享出源码 & apk文件. 注: 使用此工具需要root权限. apk文件 : http://yunpan.cn/cHPLZ8zH5BQBV (提取码:9cd2) ...

  3. 一步一步学ROP之linux_x64篇

    一步一步学ROP之linux_x64篇 一.序 **ROP的全称为Return-oriented programming(返回导向编程),这是一种高级的内存攻击技术可以用来绕过现代操作系统的各种通用防 ...

  4. ABP理论学习之NHibernate集成

    返回总目录 本篇目录 Nuget包 配置 实体映射 仓储 仓储基类 实现仓储 自定义仓储方法 阅读其他 ABP可以使用任何ORM框架工作,并且已经内置了NHibernate集成.这篇文章会解释如何在A ...

  5. CoreData教程

    网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...

  6. 将word文档A表格中的内容拷贝到word文档B表格中

    Function IsFileExists(ByVal strFileName As String) As Boolean ) <> Empty Then IsFileExists = T ...

  7. iOS block种类和切换

    block 分为三种 NSGlobalBlock,NSStackBlock, NSMallocBlock. NSGlobalBlock:类似函数,位于text段: NSStackBlock:位于栈内存 ...

  8. Linux服务器配置之加载硬盘

    Linux服务器配置之加载硬盘 1.修改密码 passwd 2.测试密码是否成功 3.查看硬盘信息 fdisk –l 4.格式化分区 fdisk /dev/vdb 5.查看分区 6.快速格式化/dev ...

  9. Docker学习笔记

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...

  10. Apache Spark源码剖析

    Apache Spark源码剖析(全面系统介绍Spark源码,提供分析源码的实用技巧和合理的阅读顺序,充分了解Spark的设计思想和运行机理) 许鹏 著   ISBN 978-7-121-25420- ...