• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

LazyInitializationException - Hibernate

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic