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. |
Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans的更多相关文章
- about hibernate lazy load and solution
about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...
- Hibernate -- lazy加载
Hibernate -- lazy加载 hibernate类级别懒加载: lazy:true(默认) //类级别懒加载 //load方法 //class lazy属性 //默认值:true load获 ...
- FW: How to use Hibernate Lazy Fetch and Eager Fetch Type – Spring Boot + MySQL
原帖 https://grokonez.com/hibernate/use-hibernate-lazy-fetch-eager-fetch-type-spring-boot-mysql In the ...
- Hibernate第一篇【介绍Hibernate,简述ORM,快速入门】
前言 前面已经学过了Struts2框架了,紧接着就是学习Hibernate框架了-本博文主要讲解介绍Hibernate框架,ORM的概念和Hibernate入门 什么是Hibernate框架? Hib ...
- Hibernate入门(4)- Hibernate数据操作
Hibernate加载数据 Session.get(Class clazz, Serializable id) clazz:需要加载对象的类,例如:User.class id:查询条件(实现了序列化接 ...
- 【Hibernate那点事儿】—— Hibernate应该了解的知识
前言: 最近由于有点时间,就像深入的学习一下Hibernate.之前只是简单的使用,并没领会它的妙处.这里就趁着分享的机会,好好整理一下. 这篇主要讲到了下面几个部分: Hibernate框架 Hib ...
- 攻城狮在路上(壹) Hibernate(十五)--- Hibernate的高级配置
一.配置数据库连接池: 1.使用默认的数据库连接池: Hibernate提供了默认了数据库连接池,它的实现类为DriverManegerConnectionProvider,如果在Hibernate的 ...
- 【Hibernate 7】浅谈Hibernate的缓存机制
一.Hibernate缓存机制简介 对于Hibernate本身来说,它的缓存主要包括三部分:session缓存(一级缓存).二级缓存.查询缓存. 1.1,session缓存 随着session的关闭而 ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)
一.结构 二.Hibernate支持的UserTypes接口 UserType —You can transform values by interacting with the plain JD ...
- hibernate报错:org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
解决方法:自定义一个Hibernate Dialect. package com.yourcompany.util ; import java.sql.Types; import org.hibern ...
随机推荐
- C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
由于项目需要,需要一个在线预览office的功能,小编一开始使用的是微软提供的方法,简单快捷,但是不符合小编开发需求, 就另外用了:将文件转换成html文件然后预览html文件的方法.对微软提供的方法 ...
- Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别
转载来源 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚. ...
- MySQL高可用架构:mysql+keepalived实现
系统环境及架构 #主机名 系统版本 mysql版本 ip地址 mysqlMaster <a href="https://www.linuxprobe.com/" title= ...
- (转载)分享常用的GoLang包工具
分享常用的GoLang包工具 包名 链接地址 备注 Machinery异步队列 https://github.com/RichardKnop/machinery Mqtt通信 github.com/e ...
- 纯数据结构Java实现(3/11)(链表)
题外话: 篇幅停了一下,特意去看看其他人写的类似的内容:然后发现类似博主喜欢画图,喜欢讲解原理. (于是我就在想了,理解数据结构的确需要画图,但我的文章写给懂得人看,只配少量图即可,省事儿) 下面正题 ...
- HBase 系列(十一)—— Spring/Spring Boot + Mybatis + Phoenix 整合
一.前言 使用 Spring+Mybatis 操作 Phoenix 和操作其他的关系型数据库(如 Mysql,Oracle)在配置上是基本相同的,下面会分别给出 Spring/Spring Boot ...
- 以太坊智能合约[ERC20]发币记录
以太坊被称为区块链2.0,就是因为以太坊在应用层提供了虚拟机,使得开发者可以基于它自定义逻辑,通常被称为智能合约,合约中的公共接口可以作为区块链中的普通交易执行.本文就智能合约发代币流程作一完整介绍( ...
- jqGrid根据数据动态设置rowList
superme.rowList = [10,20,50,100]; superme.rowNum = 20; 全局属性 loadComplete : function (data) { if(data ...
- Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)
Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...
- 《阿里巴巴Java开发手册1.4.0》阅读总结与心得(五)
笔者作为一名有数年工作经验的Java程序员,仔细研读了这份手册,觉得其是一份不可多得的好材料.阿里巴巴在发布时所说,“阿里巴巴集团推出的<阿里巴巴Java开发手册(正式版)>是阿里巴巴近万 ...


