传智播客Hibernate视频教程学习笔记34

来源:互联网 发布:吴鹰 知乎 编辑:程序博客网 时间:2024/04/29 08:32

 一对一的懒加载分析:

test.java

还是使用先前那个Person和IdCard的一对一例子例子

 

 

 

Hibernate: select person0_.id as id5_1_, person0_.name as name5_1_, idcard1_.id as id6_0_, idcard1_.userful_life as userful2_6_0_, idcard1_.person_id as person3_6_0_ from Person person0_ left outer join id_card idcard1_ on person0_.id=idcard1_.person_id where person0_.id=?

 

可以看到Hibernate在查询主对象的时候做了一个连接查询,而在查询身份证时候,缺只做了单表查询

Hibernate: select idcard0_.id as id6_0_, idcard0_.userful_life as userful2_6_0_, idcard0_.person_id as person3_6_0_ from id_card idcard0_ where idcard0_.id=?
从对象实际上是采用了懒加载

 

one-to-one懒加载条件:

1.lazy!=false

2.constrained=true

3.fetch=select

由于主表没有constrained属性,所以主表没有懒加载机制

 

fetch改为join的时候,查从对象IdCard的时候也会查找Person信息

 

缺省情况lazy=propy fetch=select,这两个条件一个是访问数据库的时机,一个是访问数据库的方式。