Problem
Unexpected error when using hibernate:
Exception in thread ' main ' org.hibernate.LazyInitializationException:could not initialize proxy-the owning session is 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)
I have a headache, the Internet to see the relevant information found such a solution:
"Org.hibernate.LazyInitializationException:could not initialize proxy" delays scratching out of the error, HB3 to many-to-one the default processing is lazy = "Proxy", Don't understand what's going on, add all the Many-to-one,one-to-one to the lazy= "false" ...
This method is true theoretically, but it will make people misunderstand, make people think just modify the value of the lazy in <set>, follow the procedure will still be an error, see the following article may be clear (note the last line of red mark)!
Lazy initialization
collections, excluding arrays, can be deferred initialization, meaning that they load their values only when the application needs access.
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!
Because before the commit, permissions was not used, the permission was not initialized, and the session was close, causing permissions to never load data.
Solution: Before the last line of a session is submitted.
Set up:
<set name= "Names" table= "names" lazy= "true" order-by= "name ASC" >
<key column= "group_id"/>
<element column= "NAME" type= "string"/>
</set>
==================================
In doing Spring1.2 + Hibernate 3 + Struts Demo time encountered a problem, edit user data time
Editing data is always not displayed correctly with the public tsysuser getuser (int userid) {} method in Com.jeedev.service.dao.hibernate.UserHibernateDao. The specific performance is:
Reference Com.jeedev.service.dao.hibernate.UserHibernateDao 第32-45 Line
Java code:
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;
}
If you delete the above
Java code:
System.out.println (Edituser.getusername ());
System.out.println (Edituser.getdeptno ());
When you click on the number, the error occurs when you modify it:
2005-10-27 15:35:59,245 ERROR [org.hibernate.LazyInitializationException]-Could not initialize Proxy-the owning Sessio N was closed
Org.hibernate.LazyInitializationException:could not initialize Proxy-the owning sessions was closed
After the Javaeye forum everyone agreed that the reason for the hibernate lazy
When applying at the web level, an unexpected lazy error occurs, and the workaround is to set the lazy= "false" example:
<class name= "Com.jeedev.hb.TSysuser" table= "T_sysuser" lazy= "false" >