Hibernate的加载抓取方式:Failed to lazily initialize a collection - no session
2015-11-06 01:31
531 查看
hibernate与数据库之间通过session交互[1]
hibernate在处理1对多的业务时很可能会出现标题所列错误.
例如
user和office是1对多
Office office = task.getOffice();
List<User> userList = office.getUserList(); //这句导致报告懒初始化集合失败
失败的原因, 目前看到是因为没有找到Hibernate交互用的会话.而会话为什么没出现?
是因为没人去建?
1 懒抓取,所以只需要找找内存
2 未在spring中创建会话
修改方法就是
1 将懒改成勤快eager
2 在spring中提供session
说道懒和勤快.Hibernate之所以喜欢懒,是因为这样的抓取,所获得的数据都在内存中(或内存实体中,例如Office这个实体里)
而勤快,意味着无实体的一次次实际sql操作.因而在频繁使用的场合,勤快会导致性能下降.
参考:
1 http://stackoverflow.com/questions/3519059/hibernate-failed-to-lazily-initialize-a-collection-of-role-no-session-or-sessi
摘录内容
If you have a large collection, you shouldn't use eager fetching. It jointly selects all data mapped to your entry and loads to memory. An alternative to this is to still use lazy fetching and open a Hibernate session each time you need to work on the related
collection, i.e, each time you need to invoke getRoleSet method. This way, Hibernate will execute the select query to database each time this method is invoked and doesn't keep the collection data in memory. You can refer to my post here for details: http://khuevu.github.io/2013/01/20/understand-hibernate.html
That's said, it can depend on your actual use case. If your collection data is small and you frequently need to query the data, you will better off using eager fetching. I think, in your specific case, a collection of role is probably quite small and suitable
to use eager fetching.
hibernate在处理1对多的业务时很可能会出现标题所列错误.
例如
user和office是1对多
Office office = task.getOffice();
List<User> userList = office.getUserList(); //这句导致报告懒初始化集合失败
失败的原因, 目前看到是因为没有找到Hibernate交互用的会话.而会话为什么没出现?
是因为没人去建?
1 懒抓取,所以只需要找找内存
2 未在spring中创建会话
修改方法就是
1 将懒改成勤快eager
2 在spring中提供session
说道懒和勤快.Hibernate之所以喜欢懒,是因为这样的抓取,所获得的数据都在内存中(或内存实体中,例如Office这个实体里)
而勤快,意味着无实体的一次次实际sql操作.因而在频繁使用的场合,勤快会导致性能下降.
参考:
1 http://stackoverflow.com/questions/3519059/hibernate-failed-to-lazily-initialize-a-collection-of-role-no-session-or-sessi
摘录内容
If you have a large collection, you shouldn't use eager fetching. It jointly selects all data mapped to your entry and loads to memory. An alternative to this is to still use lazy fetching and open a Hibernate session each time you need to work on the related
collection, i.e, each time you need to invoke getRoleSet method. This way, Hibernate will execute the select query to database each time this method is invoked and doesn't keep the collection data in memory. You can refer to my post here for details: http://khuevu.github.io/2013/01/20/understand-hibernate.html
That's said, it can depend on your actual use case. If your collection data is small and you frequently need to query the data, you will better off using eager fetching. I think, in your specific case, a collection of role is probably quite small and suitable
to use eager fetching.
相关文章推荐
- golang sync.WaitGroup解决goroutine同步
- Caused by: org.hibernate.TransactionException: unable to commit against JDBC connection
- org.hibernate.TransactionException: commit failed
- Canvas和Paint画折线图
- poj1765 November Rain 扫描线
- nat表中各条chain的工作流程
- anon vma chain
- solrcloud配置dataimport
- 【LeetCode从零单刷】Factorial Trailing Zeroes
- sleep()和 wait()有什么区别
- Hadoop YARN配置参数剖析(4)—Fair Scheduler相关参数
- AIX系统中rmdev删除设备或cfgmgr -v 时无法保存或报错,hd5的问题
- Daily Scrum 11.5
- Daily Scrum (2015/11/4)
- extmail搭建
- AIX操作系统vi命令的使用方式
- Barrier Function for constrained optimization problem
- 信号量 sem_timedwait 函数的使用
- document.domain
- 通过CM启动hive报错.org.apache.hadoop.hive.metastore.HiveMetaException: Failed to load driver