首页 > 代码库 > Hibernate —— 检索策略
Hibernate —— 检索策略
一、Hibernate 的检索策略本质上是为了优化 Hibernate 性能。
二、Hibernate 检索策略包括类级别的检索策略、和关联级别的检索策略(<set> 元素)
三、类级别的检索策略
1. 立即检索、延迟检索
2. 通过 <class> 节点的 lazy 属性来控制。默认为 true,即为延迟检索。
3. 只针对 session 的 load() 方法生效。
默认情况下,通过 load() 方法获取到的对象是一个代理对象,Hibernate 创建代理对象时,仅会初始化 OID。
在第一次访问非 OID 属性时,Hibernate 会初始化代理类实例。
4.测试
(1)<class> 的 lazy 属性为 true
@Testpublic 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_05--------------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
@Testpublic 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 时(增强的延迟检索策略),会尽可能的延迟初始化集合的时机。如:
@Testpublic 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"
@Testpublic 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=?2Hibernate: 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=?2Hibernate: 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=?2Hibernate: 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 ( ?, ? )22
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_ )2222
通过子查询的方式,通过 in 的方式。
<3>取值为 "join"
- 会采用迫切左外链接(使用左外链接进行查询,同时初始化集合属性)策略来初始化所有关联的对象
- lazy 属性将会被忽略
- Query 的 list() 方法会忽略这个取值,不会忽略 lazy 属性。
- HQL 会忽略取值为 "join" 的取值
@Testpublic 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)默认情况下
@Testpublic 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=?2Hibernate: 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"
@Testpublic 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 ( ?, ? )22
五、<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 —— 检索策略