关于在配置pojo类中的懒加载的异常分析



延迟抓取出的错,hb3对many-to-one的默认处理是lazy ="proxy",把所有many-to-one,one-to-one都加上lazy="false"...

今天遇到了这样一个问题

 

Struts Problem Report

Struts has detected an unhandled exception:

Messages:
  • failed to lazily initialize a collection of role: feng.domain.Department.users, no session or session was closed
File:org/hibernate/collection/AbstractPersistentCollection.java
Line number:383

Stacktraces

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: feng.domain.Department.users, no session or session was closed
    org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:122)
    org.hibernate.collection.PersistentSet.size(PersistentSet.java:162)
"org.hibernate.LazyInitializationException: could not initializeproxy"


在网上找了好久终于找到了一个较全的解决方法。具体如下:

 

 

  "org.hibernate.LazyInitializationException: could not initializeproxy"延迟抓取出的错,hb3对many-to-one的默认处理是lazy ="proxy",把所有many-to-one,one-to-one都加上lazy="false"...

 这个方法理论上没错,但是会让人产生误解,使人以为只是修改<set>中的lazy值,照做后依然会报错,看看下面的文章也许就明白了(注意最后一行红色标记)!

  lazy initialization

  集合(不包括数组)是可以延迟初始化的,意思是仅仅当应用程序需要访问时,才载入他们的值。

Java代码   收藏代码
  1. <span style="font-family: monospace;">s = sessions.openSession();  
  2. User u = (User) s.find("from User u where u.name=?", userName,  
  3. Hibernate.STRING).get(0);  
  4. Map permissions = u.getPermissions();  
  5. s.connection().commit();  
  6. s.close();  
  7. Integer accessLevel = (Integer) permissions.get("accounts"); //Error!</span>  

  因为在commit之前,permissions没有被用到,permission没有被初始化,而session被close了,导致permissions永远都无法load data.

  解决办法:吧最后一行一道session被提交之前.

  设置:

Java代码   收藏代码
  1. <span style="font-family: monospace;"><set name="names" table="NAMES" lazy="true" order-by="name asc">  
  2. <span style="white-space: pre;">    </span><key column="group_id"/>  
  3. <span style="white-space: pre;">    </span><element column="NAME" type="string"/>  
  4. </set></span>  

  ==================================

  在做Spring1.2 + Hibernate 3 + struts demo时候遇到一个问题,编辑用户数据时候

  用com.jeedev.service.dao.hibernate.UserHibernateDao 中 public TSysuser getUser(int userid) { } 方法,总是不能正常显示编辑数据。具体表现为:

  参考 com.jeedev.service.dao.hibernate.UserHibernateDao 第32-45行

  java代码

  1. <span style="font-family: monospace;">public TSysuser getUser(int userid) {  
  2.   if (this.getHibernateTemplate() == null) {  
  3. System.out.println("error at there");  
  4. return null;  
  5. }  
  6.   TSysuser edituser= (TSysuser) getHibernateTemplate().load(TSysuser.classnew Integer(userid));  
  7.   System.out.println(edituser.getUsername());  
  8. System.out.println(edituser.getDeptno());  
  9.   return edituser;  
  10. }</span>  

  如果删除上面的java代码:

 


  1. <span style="font-family: monospace;">System.out.println(edituser.getUsername());  
  2. System.out.println(edituser.getDeptno());</span>  

 

 

  在点击编号,修改时候就会出现错误:

  [org.hibernate.LazyInitializationException] - could not initialize proxy - the owning Session was closed

Java代码   收藏代码
  1. org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed  
 

  经过在javaeye论坛大家一致认为 是hibernate lazy的原因

  在WEB级别应用时候,会出现意想不到的lazy错误,解决方法就是 设置 lazy="false" 例:

  <hibernate-mapping>

Java代码   收藏代码
  1. <class name="com.jeedev.hb.TSysuser" table="t_sysuser" lazy="false">  
 

 

还有一种解决方法

2、对于查询中如果用的是xxx.load(class,id)则改为xxx,get(class,id)。在web.xml文件中加入

Java代码   收藏代码
  1. <filter>  
  2.   <filter-name>hibernateFilter</filter-name>  
  3.   <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  4.   
  5.   <init-param>  
  6.             <param-name>singleSession</param-name>  
  7.             <param-value>false</param-value>  
  8.         </init-param>   
  9.   
  10. <!-- 这个--  <init-param>一定要加不然很可能会报 错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition  
  11. >  
  12. </filter>  
  13.   
  14. <filter-mapping>  
  15.   <filter-name>hibernateFilter</filter-name>  
  16.   <url-pattern>*.mmg</url-pattern>  
  17. </filter-mapping> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值