org.hibernate.LazyInitializationException: could not initialize proxy - no

简介: 错误页面提示could not initialize proxy - no Session控制台org.hibernate.LazyInitializationException: could not initialize proxy - no Session病症:这是一个lazy使用后的Exception,使用迟时加载,在session(hibernate里的session),关闭后使用该对象的未加载变量,也就是说session已经关闭,没有保存到内存中,然后你使用了,导致该异常。

错误页面提示

could not initialize proxy - no Session

控制台

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

病症:这是一个lazy使用后的Exception,使用迟时加载,在session(hibernate里的session),关闭后使用该对象的未加载变量,也就是说session已经关闭,没有保存到内存中,然后你使用了,导致该异常。


Q:我以get得到一个对象,在JSP页面以EL表达它的实例变量时,也抛出了该异常,在application的测试时却没有出现。
A:推测可能是该实例变量是hibernate进行加载的,可能hibernate本身是以load的方式将该实例变量得到。
解决方法:

方法1 .用openSessionInView 
方法2. 把这个类的延迟加载禁掉

list=query.list();for (Object object : list) 
     Hibernate.initialize(((Deliver)object).getPlnbill());//Deliver是复合对象,有属性引用于PlnBill

OR        

resultExchangeRatePojo.getDollarRmb();//在它的实例变量DollarRmb的session未关闭前存入,实际上将它载入内存。(我就想在没用关闭session之前用一下就可以把值保存到内存中了)

结果:果然页面上就正常显示了。


总结:原因是hibernate的session已经关闭,集合没有被初始化。在hibernate中:hibernate3 默认支持延迟加载(lazy="proxy"我们可以把proxy看作是true),hibernate2 默认立即加载 (lazy="false")。

      在hibernate3中,所有的实体设置文件(user.hbm.xml)中的lazy属性都被默认设成了true,就是当这个类没有被调用时,延时加载,导致了以上情况的发生,在配置文件中将lzay属性设为false就可以了。

----------------------------------------------------------------

原因 :
<may-to-one>or<one-to-may> 的 lazy 属性默认为 :lazy = "proxy"
解决 :<many-to-one>   & <set> 中设置 lazy="false" 

如果还不行,根据自己需求,经过我的仔细排查放在set一端不行,那就放在<many-to-one>那端..

相关文章
|
8月前
|
Java
ApplicationContext is unlikely to start due to a @ComponentScan of the default package
解决办法: 1、一般发出这个警告的原因是你把启动类直接放在的src目录下面。 2、你需要在src目录下面再建一个包,然后把启动类放到下面。 3、或者你错将启动类放到java文件中了,与项目包成了同级文件,你只需要将启动类放到项目包里就可以。
39 0
|
10月前
|
Java 数据库连接
org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration
Error creating bean with name 'entityManagerFactory' defined in file [E:\eclipseworkspace\wms_ims\.metadata\.plugins\org.eclipse.wst.server.core\tmp9\wtpwebapps\shopping\WEB-INF\classes\applicationContext-configuration.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMetho
73 0
|
10月前
|
SQL
org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration
org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration
66 0
|
11月前
|
关系型数据库 MySQL 数据库
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
199 0
|
Java Spring
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
|
Java 数据库连接 数据库
### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating
在写数据库的url地址时用了“&”符号,将“&”换成“&;”
164 0
### Error building SqlSession. ### Cause: org.apache.ibatis.builder.BuilderException: Error creating
|
XML Java 数据库连接
springboot整合mybatis中:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
在项目的启动过程中如果出现:以下报错信息,则可以从以下几个方面入手排查问题。 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因是Mapper interface和xml文件的定义对应不上,需要检查包名,namespace,函数名称等能否对应上,需要比较细致的对比,这是第二次出现这种异常。我每次都是按照如下方法去逐一排查的,到最后都能解决问题。
648 0
|
存储 NoSQL Java
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration报错问题处理
java.lang.IllegalStateException: Cannot load configuration class: org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
hibernate4 could not initialize proxy - no Session
hibernate4 could not initialize proxy - no Session
|
Java 数据库连接
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hi
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hi
128 0