覆盖 OneToMany 关联中的延迟获取
我一直面临 LazyInitializationException 并通过谷歌搜索得到了三种解决方案:
- 在 getMyList() 中添加 myList.size()
- 覆盖延迟获取(连接获取)
- 急切获取
我正在避免急切获取,并且想知道急切之间有什么区别抓取并加入抓取。谁能告诉我最好的解决方案是什么?
I've been facing the LazyInitializationException and had three solutions by googling it:
- add myList.size() in getMyList()
- override the lazy fetch (join fetch)
- eager fetch
i'm avoiding the eager fetching and was wondering what's the difference between eager fetching and join fetch. could anyone tell me what's the best solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的解决方案可能是您没有列出的解决方案:在视图模式中打开会话。 Spring 以 Servlet 过滤器 或 方法拦截器。
急切的获取描述了关系的惰性,或者何时发生获取。连接抓取描述了抓取策略,或者抓取如何发生。两者是不同的概念。 第 21.1 节 《Hibernate 参考指南》对如何以及何时进行详细说明,以及许多其他有用的花絮。
The best solution is likely one that you didn't list: the open session in view pattern. Spring provides a very convenient implementation of the pattern in the form of either a Servlet Filter or a method interceptor.
Eager fetching describes the laziness of a relationship, or when fetching happens. Join fetching describes the fetch strategy, or how fetching happens. The two are separate concepts. Section 21.1 of the Hibernate Reference Guide has a full description of the how and when bit along with a lot of other useful tidbits.