Hibernate —— 检索策略
一、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 —— 检索策略的更多相关文章
- hibernate(八) Hibernate检索策略(类级别,关联级别,批量检索)详解
序言 很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要 ...
- Hibernate 检索策略
概述 检索数据时的 2 个问题: –不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象, 而程序实际上仅仅需要访问 Custome ...
- Hibernate检索策略(抓取策略)(Hibernate检索优化)
一.查询方法中get方法采用策略是立即检索,而load方法采用策略是延迟检索,延迟检索是在使用数据时才发送SQL语句加载数据 获取延迟加载数据方式:1.使用的时候,如果Customer c=sessi ...
- [原创]java WEB学习笔记88:Hibernate学习之路-- -Hibernate检索策略(立即检索,延迟检索,迫切左外连接检索)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Hibernate检索策略
1. Hibernate的检索策略概述: 检索数据时的 2 个问题: 1.不浪费内存:当 Hibernate 从数据库中加载 Customer 对象时, 如果同时加载所有关联的 Order 对象 ...
- (九)Hibernate 检索策略
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 这里的hibernate.cfg.xml配置信息我就不再写了 第一节:检 ...
- Hibernate学习(八)———— Hibernate检索策略(类级别,关联级别,批量检索)详解
序言 很多看起来很难的东西其实并不难,关键是看自己是否花费了时间和精力去看,如果一个东西你能看得懂,同样的,别人也能看得懂,体现不出和别人的差距,所以当你觉得自己看了很多书或者学了很多东西的时候,你要 ...
- hibernate检索策略(抓取策略)
检索策略 类级别检索 默认检索策略:默认延迟加载, 可以使用lazy属性来进行改变. session.get(clazz,object)默认立即加载 @Test //测试左外连接查询 public v ...
- Hibernate检索策略与检索方式
hibernate的Session在加载Java对象时,一般都会把鱼这个对象相关联的其他Java对象也都加载到缓存中,以方便程序的调用.但很多情况下,我们不需要加载太多无用的对象到缓存中,一来会占用大 ...
随机推荐
- python读取文本文件
1. 读取文本文件 代码: f = open('test.txt', 'r') print f.read() f.seek(0) print f.read(14) f.seek(0) print f. ...
- PHP 真正多线程的使用
以前使用curl的多线程并不是真正的多线程,只是一种模拟的多线程,现在使用pthreads来实现真正意义上的多线程. 下载: windows下: http://windows.php.net/down ...
- HTML的两三事
关于在页面布局上,我用了两种方式 用 来调整需要空格的地方, 用<pre></pre>直接在代码页面调整页面布局 但是用 来布局当我把网页 ...
- 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
[题外话] 上次介绍了VS上开发Node.js的插件Node.js Tools for Visual Studio(NTVS),其提供了非常方便的开发和调试功能,当然很多情况下由于平台限制等原因需要在 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(2)
在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...
- Worktile 技术架构概要
其实早就该写这篇博客了,一直说忙于工作没有时间,其实时间挤挤总会有的,可能就是因为懒吧!从2013年11月一直拖到现在,今天就简单谈谈 Worktile 的技术架构吧 . Worktile 自上线到现 ...
- C语言 · 矩阵乘法
问题描述 输入两个矩阵,分别是m*s,s*n大小.输出两个矩阵相乘的结果. 输入格式 第一行,空格隔开的三个正整数m,s,n(均不超过200). 接下来m行,每行s个空格隔开的整数,表示矩阵A(i,j ...
- 《Entity Framework 6 Recipes》中文翻译系列 (46) ------ 第八章 POCO之领域对象测试和仓储测试
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 8-8 测试领域对象 问题 你想为领域对象创建单元测试. 这主要用于,测试特定的数 ...
- JS设计模式(一)
刚入职时,看过一段时间的设计模式,似懂非懂.不知不觉过去七个月了,对JS的理解更深刻了,数据结构与算法的基础也基本上算是过了一遍了,接下来要把设计模式搞定,然后不再深层次研究JS了,而是学习前端自动化 ...
- Android开发学习之路-RecyclerView使用初探
在进行一些MaterialDesign规范开发的时候,比如之前说到的CoordinateLayout实现的向上折叠效果的时候,如果依然使用ListView,那么这种效果是做不出来的,因为ListVie ...