• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LazyInitializationException - Hibernate

 
Venkatesh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All:

I am a newbie to Hibernate and am getting the following exception when trying to run a sample pgm from the Hibernate tutorial.Can anyone pl let me know on how to resolve this exception.

------------------------------------------------------------------------
[java] org.hibernate.LazyInitializationException: failed to lazily initiali
ze a collection of role: Person.events - no session or session was closed
[java] at org.hibernate.collection.AbstractPersistentCollection.throwLa
zyInitializationException(AbstractPersistentCollection.java:191)
[java] at org.hibernate.collection.AbstractPersistentCollection.initial
ize(AbstractPersistentCollection.java:183)
[java] at org.hibernate.collection.AbstractPersistentCollection.read(Ab
stractPersistentCollection.java:48)
[java] at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.
java:118)
[java] at EventManager.main(Unknown Source)
[java] Exception in thread "main"
[java] Java Result: 1
[echo] Executing
------------------------------------------------------------------------

part of the code in my main():
------------------------------------------------------------------------

mgr.addPersonToEvent(new Long(1), new Long(1));
mgr.addPersonToEvent(new Long(1), new Long(2));

List personList = mgr.listPersons();
for (int i = 0; i < personList.size(); i++)
{
Person person = (Person) personList.get(i);
Set eventSet = person.getEvents();
if (eventSet != null && !eventSet.isEmpty())
{
Iterator iter = eventSet.iterator();
while (iter.hasNext())
{
System.out.println("Event - " + ((Event)
iter.next()).getTitle());
}
}
}

private void addPersonToEvent(Long personId, Long eventId)
{
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

Person aPerson = (Person) session.load(Person.class, personId);
Event anEvent = (Event) session.load(Event.class, eventId);
aPerson.getEvents().add(anEvent);

tx.commit();
HibernateUtil.closeSession();
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

no session or session was closed

You need a open Session to get more data from the database. Either explicitly get the events association when you get your persons object, or access them from whithin an open session.
 
Wagner Danda Da Silva Filho
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a lot of people with this misunderstanding about Hibernate session. Many people are using in their web applications the SessionInView Pattern, opening the hibernate session before any jsp/java processing and closing it after having shown the jsp page. If you intend to use Struts, you can create a HibernateFilter. Search at google and you'll find some good examples.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic