Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans
I have been suffering from infamous hibernate exception
Now the community is cheering over
saying it solves the problem but USE IT WITH CAUTION. What they mean by use it with caution? What this property actually does? Please give me any insights. Thanks in advance. |
|||||||||
|
The problem with this approach is that you can have the N+1 effect. Imagine that you have the following entity:
If you have a report that returns 10K of persons, and if in this report you execute the code Imagine now that Order is like below:
Imagine now that you have a iteration with the For LazyInitializationException you can have some solutions:
If you still have any problem, check this link: http://uaihebert.com/four-solutions-to-the-lazyinitializationexception |
|||||||||||||||||||||
|
Did you find this question interesting? Try our newsletter
Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).
This goes against how we can take advantage of Hibernate's enforcement of repeatable read semantics with the Session concept. When an object is first loaded and if the object is referenced again within the life of the session, then the same object is returned IRRESPECTIVE of whether this object has changed in the DB. This is the repeatable read semantics provided automatically by hibernate. With this setting, you have no session providing this guarantee, so if you now access this data you will be getting the latest version of the data. This might be fine. But consider the scenario where this object is held in some place for a long time and the data has changed considerably, so that the lazily fetched data is much different that the data already loaded when the session was alive. This is what you need to be concerned about.
But if this (your program is exposed to timing issues, when it might work fine one time and fail another time) is a concern, then fetch all the necessary data while in session. |
|||
Probably because there are better solutions, like @Transactional, where opening and closing sessions follows a very common pattern of "open a session then wrap everything in a try-catch-finally; catch rolls back and finally closes the session." This annotation is typically at the request-level for web apps and services. Or if you need more granular control you can open sessions manually using a SessionFactory. And as others have mentioned, lazy-loading is something you need to be aware of. It's not a silver bullet but it can be very helpful. Generally, if your apps are designed to have many small requests then its ok. Eager loading can also be very bad. For example, when your object model has lots of many-to-many relationships but your requests don't use data more than one level deep. Or you can just forget the whole thing for now. Use lazy loading until it becomes an issue. And if it does, you would have been better of with Mybatis anyway. |
最新文章
- asp.netDataTable导出excel方法(1)
- 在Go语言中使用JSON(去掉空字段)
- winScp如何通过隧道代理进行远程连接
- maven编译项目时提示:cached in the local repository
- TypeScript 素描 - 模块
- C#中的AssemblyInfo 程序集信息
- Emgu学习笔记(一)安装及运行Sample
- dfs和bfs的简单总结
- redis编译 报告错误 jemalloc/jemalloc.h:没有那个文件或目录 解决.
- mockplus 原型设计工具
- [Swift]LeetCode161. 一次编辑距离 $ One Edit Distance
- 学习一下sticky-footer
- linux的基本操作2
- CentOS上部署.net core
- MySQL中的编码问题
- 团队作业第六次——团队Github实战训练
- acm模板生成
- grpc python quickstart
- python学习——大文件分割与合并
- ubuntu 玩转 nodejs