-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: org.hibernate.LazyInitializationException
PostPosted: Wed Apr 20, 2005 11:57 am 
Newbie

Joined: Wed Apr 20, 2005 7:02 am
Posts: 3
I'm new in Hibernate i've got source like this when i compile the error occure


Session sesja = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory().openSession();
Transaction tx = sesja.beginTransaction();
Kategoria koc = (Kategoria) sesja.get(Kategoria.class, new Long(1));
tx.commit();
sesja.close();

Kategoria kat = new Kategoria();
kat.setKategoriaNazwa("Koza");
// here the error start
koc.getSetOfKategoria().add(kat);
kat.setKategoria(koc);

the error what i made wrong ?

11527 [main] ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection (com.manabit.model.Kategoria.SetOfKategoria) - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection (com.manabit.model.Kategoria.SetOfKategoria) - no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:180)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
at com.manabit.cwiczenia.Kategorie.main(Kategorie.java:43)
Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection (com.manabit.model.Kategoria.SetOfKategoria) - no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:180)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:61)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:158)
at com.manabit.cwiczenia.Kategorie.main(Kategorie.java:43)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 3:06 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Well, it told you exactly what the problem is - your session is closed. Most likely cuases are: a) you closed the sesion earlier, b) the object is detached. Most likely, it's b.

Objects become detached when the session that loaded them closes. In order to use the object with a new session, you must call session.update() or session.lock() to bind the object to the session.

A great way to prevent this is to use the Open Session In View (search the site and the forums). If you're a GUI, then the Thread Local pattern might do, which the Open Session in View pattern is based on.

Also, buy Hibernate in Action. It has chapters on lazy objects, detached persistent objects, and the open session in view pattern.

Also try to identify if you are using the anti-patter: Session per operation.[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 3:08 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
I just looked more closely at your code.

You are closing your session and then trying to access lazy associations. Keep the session open for the whole operation.

My earlier suggestions still hold true, however. Session management should be off-loaded to a process outside your data access code. I still recommend the Thread Local or Open Session in View patterns to manage your Hibernate Sessions. Manually opening and closing them is not only tedius, but it's asking for errors.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 3:20 pm 
Newbie

Joined: Wed Apr 20, 2005 7:02 am
Posts: 3
thanks for replay i now that i close the session
i read now Hibernate in Action

Session = getSession();
Tr ...
Category computer = Session.get(Category.class, new Long(1));
...
Session.close();


// here i've got the same problem // what i understood that
computer.getChildrenCategories().add(laptops); // this section must be independent we work ony on object which is independet from session
laptops.setParentCategory(computer); //


Session = getSession();
Tr ...

session.save(laptops);
...
Session.close();


maybe i do somethink wrong ??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 5:17 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Hi
You have to close the session only when you finsih your entire transaction. You cant say session.close and still try to set the value for the child and save.
The process should be done with in one entrire session for Lazy loading ie if you have enable lazy=true

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 1:58 pm 
Newbie

Joined: Wed Apr 20, 2005 7:02 am
Posts: 3
thnx


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.