Explain a question about hibernate lazy.

Source: Internet
Author: User
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" >



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.