hibernate检索策略(抓取策略)
检索策略
类级别检索
默认检索策略:默认延迟加载, 可以使用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 |
|
||||||
fetch |
|
未进行设置则默认为懒加载
@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 |
|
||||||
lazy |
|
hibernate检索策略(抓取策略)的更多相关文章
- Hibernate(十四)抓取策略
抓取策略: 抓取策略是当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.Hibernate的抓取策略是Hibernate提升性能的一 ...
- hibernate 延迟加载和抓取策略
一.延迟加载 1.简单查询get,load 针对对象本身延迟或即时 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load ...
- Hibernate学习---第十一节:Hibernate之数据抓取策略&批量抓取
1.hibernate 也可以通过标准的 SQL 进行查询 (1).将SQL查询写在 java 代码中 /** * 查询所有 */ @Test public void testQuery(){ // ...
- 【转】hibernate延迟加载和抓取策略
一.延迟加载 1.简单查询get,load 针对对象本身延迟或即时 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load ...
- 【Hibernate学习】 —— 抓取策略(注解方式)
当应用程序须要在关联关系间进行导航的时候.hibernate怎样获取关联对象的策略. 抓取策略的方式: FetchType.LAZY:懒载入.载入一个实体时.定义懒载入的属性不会立即从数据库中载入. ...
- Hibernate查询方式&抓取策略
Hibernate的查询方式 1.OID查询 hibernate根据对象的OID(主键)进行检索 使用get方法 Customer customer=session.get(Customer.clas ...
- 【Hibernate 8】Hibernate的调优方法:抓取策略
在上一篇博客中,介绍了Hibernate的缓存机制.合理的配置缓存,可以极大程度上优化Hibernate的性能.这篇博客,介绍另外一个调优方式:抓取策略. 一.什么是抓取策略 抓取策略(fetchin ...
- Hibernate中的多表查询及抓取策略
1.Hibernate中的多表查询 1.1SQL中的多表查询 [交叉连接] select * from A,B; [内连接] 显示内连接:inner join(inner 可以省略) Select * ...
- Hibernate的抓取策略(优化)
延迟加载的概述 什么是延迟加载 延迟加载:lazy(懒加载).执行到该行代码的时候,不会发送语句去进行查询,在真正使用这个对象的属性的时候才会发送SQL语句进行查询. 延迟加载的分类 l 类级别的延 ...
随机推荐
- C#读取Excel表中的数据时混合字段部分数据没有
解决同一列中“字符串”和“数字”两种格式同时存在,读取时,不能正确显示“字符串”格式的问题:set xlsconn=CreateObject("ADODB.Connection") ...
- vue.js路由vue-router
学习网址:https://segmentfault.com/blog/vueroad 转载至:https://segmentfault.com/a/1190000009350679#articleHe ...
- 文章如何做伪原创 SEO大神教你几招做"原创"网站文章的心得
想要创作出好的文章并被百度所喜欢,就非常需要SEO的优化能力,以及要对文章进行塬创或伪塬创,那么,如何做伪塬创文章?以及如何做好塬创网站文章呢?对此,本文小编就为大家带来了几招做"塬创&qu ...
- Svn启动窗口报错 Could not load file or assembly 'SharpSvn.dll' or one of its
win10 64位系统生成没问题,测试都没问题,结果换到win7 64位系统上,点开就出现,网上搜了下,通过以下方式解决, 必须把bin 文件夹全部删除,重新生成.要不还是会报错. Solve it. ...
- export export defalut
require/exports 和 import/export 形式不一样 require/exports 的用法只有以下三种简单的写法: const fs = require('fs') expor ...
- 20172325 2018-2019-2 《Java程序设计》第五周学习总结
20172325 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 本次学习第九章内容,主要学习查找和排序. 查找 查找的定义:是一个过程,即在某个项目组中寻找 ...
- vim中代码自动格式化
参考资料: https://blog.csdn.net/qachenzude/article/details/25511875 1,gg 跳转到第一行 2,shift+v 转到可视模式 3,shift ...
- apicloud模块开发知识点
1. 没有加模块的时候dex里面的包 \android\support\annotation \android\support\v4 \com\uzmap\pkg \compile 2. 不能混淆的类 ...
- 微信小程序获取当前位置
详细参数说明请看小程序api文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.openLocation.html wx.getLoc ...
- ERROR - abandon connection, open stackTrace
项目采用的是阿里巴巴的druid连接池,配置文件中有个removeAbandoned的配置(意义是某个连接如果超过设置的连接活动时间的话,连接会被强制关掉),但是因为爬虫中某些连接会长时间处于活动状态 ...