例子:一个User表对应多个file
表user : id name
表file : id name uid
对应对象文件
User
File
调用代码
情况有3种
第一种:在User那个文件里@OneToMany(mappedBy="user") 不设置是否去取set
结果:
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_ from user user0_ where user0_.id=?
------------Id-----------7
------------Id-----------xiugaile o
报错:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.tony.data.dto.User.fileList, no session or session was closed
第二种:在User那个文件里 @OneToMany(mappedBy="user",fetch=FetchType.EAGER)
Hibernate: select user0_.id as id0_1_, user0_.name as name0_1_, filelist1_.uid as uid3_, filelist1_.id as id3_, filelist1_.id as id1_0_, filelist1_.name as name1_0_, filelist1_.uid as uid1_0_ from user user0_ left outer join file filelist1_ on user0_.id=filelist1_.uid where user0_.id=?
------------Id-----------7
------------Id-----------xiugaile o
------------Id-----------[com.tony.data.dto.File@12f1bf0, com.tony.data.dto.File@e1dac2, com.tony.data.dto.File@1d32e45]
没异常
第三种 在User那个文件里 @OneToMany(mappedBy="user",fetch=FetchType.lazy)
同第一种一样 从而默认是延迟加载的呢。
这个判断的环境是使用hibernate和spring 结合用的是hibernate的注解方式
在该条件下以上结果成立。