<b>一个关于Hibernate lazy的问题</b>[Java编程]
本文“<b>一个关于Hibernate lazy的问题</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
利用Hibernate时不测报错:
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.entry.database.bean.OaLoginLink$EnhancerByCGLIB$a418b18d.getName(<generated>)
at test.Test.main(Test.java:145)
非常让我头痛,上网查看相关资料发现这样一个办理方法:
"org.hibernate.LazyInitializationException: could not initialize proxy"耽误抓取出的错,hb3对many-to-one的默许处理是lazy = "proxy",没有搞懂到底怎么回事,把全部many-to-one,one-to-one都加上lazy="false"...
这个办法理论上没错,但是会让人产生曲解,令人认为只是改正<set>中的lazy值,照做后程序仍然会报错,看看下面的文章大概就懂得了(注意最后一行红色标志)!
lazy initialization
调集(不包含数组)是可以耽误初始化的,意思是仅仅当利用程序需求拜候时,才载入他们的值.
s = sessions.openSession();
User u = (User) s.find("from User u where u.name=?", userName,
Hibernate.STRING).get(0);
Map permissions = u.getPermissions();
s.connection().commit();
s.close();
Integer accessLevel = (Integer) permissions.get("accounts"); //Error!
因为在commit之前,permissions没有被用到,permission没有被初始化,而session被close了,招致permissions永久都无法load data.
办理办法:吧最后一行一道session被提交之前.
设置:
<set name="names" table="NAMES" lazy="true" order-by="name asc">
<key column="group_id"/>
<element column="NAME" type="string"/>
</set>
==================================
在做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代码:
public TSysuser getUser(int userid) {
if (this.getHibernateTemplate() == null) {
System.out.println("error at there");
return null;
}
TSysuser edituser= (TSysuser) getHibernateTemplate().load(TSysuser.class, new Integer(userid));
System.out.println(edituser.getUsername());
System.out.println(edituser.getDeptno());
return edituser;
}
假如删除上面的
java代码:
System.out.println(edituser.getUsername());
System.out.println(edituser.getDeptno());
在点击编号,改正时刻就会呈现错误:
2005-10-27 15:35:59,245 ERROR [org.hibernate.LazyInitializationException] - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
经过在javaeye论坛大家一致认为 是hibernate lazy的缘由
在WEB级别利用时刻,会呈现意想不到的lazy错误,办理办法就是 设置 lazy="false" 例:
<hibernate-mapping>
<class name="com.jeedev.hb.TSysuser" table="t_sysuser" lazy="false">
以上是“<b>一个关于Hibernate lazy的问题</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |