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对象也都加载到缓存中,以方便程序的调用.但很多情况下,我们不需要加载太多无用的对象到缓存中,一来会占用大 ...
随机推荐
- ruby 访问新浪微博API post方式和get方式
require 'net/https' require 'uri' def post_api(api, args) uri = URI.parse api http = Net::HTTP.new(u ...
- IEEE/ACM ASONAM 2014 Industry Track Call for Papers
IEEE/ACM International Conference on Advances in Social Network Analysis and Mining (ASONAM) 2014 In ...
- xcodebuild编译ipa
#!/bin/sh # autoBuild.sh # CTest # # Created by Ethan on 14-11-3. # Copyright (c) 2014年 Ethan. All r ...
- 一张图解释SQL Server集群、镜像、复制、日志传送
一张图解释SQL Server集群.镜像.复制.日志传送 本文版权归作者所有,未经作者同意不得转载.
- 基于zookeeper实现统一资源管理
分布式系统中经常涉及到配置资源的管理,比如,一个应用系统需要部署在多台服务器上,但是他们拥有某些的配置项是相同的,如果配置变更,需要修改这些配置,那么需要同时修改每台服务器,这样做比较麻烦而且容易出错 ...
- 剑指Offer面试题:23.二叉树中和为某一值的路径
一.题目:二叉树中和为某一值的路径 题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.例如输入下图中二叉树和整数2 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (30) ------ 第六章 继承与建模高级应用之多对多关联
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第六章 继承与建模高级应用 现在,你应该对实体框架中基本的建模有了一定的了解,本章 ...
- 《Node web开发》笔记
还是因为学习kibana,才开始了解node. Node是一种基于事件驱动的异步系统,基于Chrome的引擎V8. Node中由于大量的使用模块,因此出现了很多开源模块,有点像java社区的样子. 笔 ...
- Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...
- 【源码】谷歌代理~WPF简单小软件-2015-10-15首发 (2016-03-01已更新源)
蛋疼,昨天把代理去了后才发现,原来咱们连谷歌应用都访问不了,,,用别人的总觉得不怎么安全,然后今天早上就编了个小软件干掉他这限制==> GoogleProxy.exe [主要目的:为了能在线安 ...