hql的join fecth 和lazy

本文对比了Hibernate中Fetch与Lazy加载策略的区别,特别是在多对一关系中的应用。通过具体示例说明了两种策略下SQL查询的差异,展示了Fetch如何通过单次查询加载所有相关对象。

使用关联查询的时候:

ex:

user和person是多对一的关系 person和organization是多对一的关系

select u from User u left join u.person p where u.id = 1

如果lazy为true,不初始化person类中的父类organization

Hibernate: select user0_.id as id4_, user0_.username as username4_, user0_.password as password4_, user0_.createTime as createTime4_, user0_.expireTime as expireTime4_, user0_.person as person4_ from t_user user0_ left outer join t_person person1_ on user0_.person=person1_.id where user0_.id=1


如果lazy为false,初始化organization

Hibernate: select user0_.id as id4_, user0_.username as username4_, user0_.password as password4_, user0_.createTime as createTime4_, user0_.expireTime as expireTime4_, user0_.person as person4_ from t_user user0_ left outer join t_person person1_ on user0_.person=person1_.id where user0_.id=1
Hibernate: select person0_.id as id1_2_, person0_.name as name1_2_, person0_.sex as sex1_2_, person0_.age as age1_2_, person0_.address as address1_2_, person0_.duty as duty1_2_, person0_.phone as phone1_2_, person0_.org as org1_2_, organizati1_.id as id0_0_, organizati1_.name as name0_0_, organizati1_.sn as sn0_0_, organizati1_.description as descript4_0_0_, organizati1_.pid as pid0_0_, organizati2_.id as id0_1_, organizati2_.name as name0_1_, organizati2_.sn as sn0_1_, organizati2_.description as descript4_0_1_, organizati2_.pid as pid0_1_ from t_person person0_ left outer join waj_organization organizati1_ on person0_.org=organizati1_.id left outer join waj_organization organizati2_ on organizati1_.pid=organizati2_.id where person0_.id=?



select u from User u left join fetch u.person p where u.id = 1 

fetch"连接允许仅仅使用一个选择语句就将相关联的对象或一组值的集合随着他们的父对象的初始化而被初始化,这种方法在使用到集合的情况下尤其有用,对于关联和集合来说,它有效的代替了映射文件中的外联接 与延迟声明(lazy declarations). 

Hibernate: select user0_.id as id4_0_, person1_.id as id1_1_, user0_.username as username4_0_, user0_.password as password4_0_, user0_.createTime as createTime4_0_, user0_.expireTime as expireTime4_0_, user0_.person as person4_0_, person1_.name as name1_1_, person1_.sex as sex1_1_, person1_.age as age1_1_, person1_.address as address1_1_, person1_.duty as duty1_1_, person1_.phone as phone1_1_, person1_.org as org1_1_ from t_user user0_ left outer join t_person person1_ on user0_.person=person1_.id where user0_.id=1
Hibernate: select organizati0_.id as id0_1_, organizati0_.name as name0_1_, organizati0_.sn as sn0_1_, organizati0_.description as descript4_0_1_, organizati0_.pid as pid0_1_, organizati1_.id as id0_0_, organizati1_.name as name0_0_, organizati1_.sn as sn0_0_, organizati1_.description as descript4_0_0_, organizati1_.pid as pid0_0_ from waj_organization organizati0_ left outer join waj_organization organizati1_ on organizati0_.pid=organizati1_.id where organizati0_.id=?

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值