Hello, I have downloaded Hibernate3 and have been working through the tutorial at
http://www.hibernate.org/hib_docs/v3/re ... orial.html. Towards the bottom of the tutorial it states:
Quote:
You might of course load person and event in different units of work. Or you modify an object outside of a Session, when it is not in persistent state (if it was persistent before, we call this state detached). In (not very realistic) code, this might look as follows:
Code:
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);
tx.commit();
HibernateUtil.closeSession();
aPerson.getEvents().add(anEvent); // aPerson is detached
Session session2 = HibernateUtil.currentSession();
Transaction tx2 = session.beginTransaction();
session2.update(aPerson); // Reattachment of aPerson
tx2.commit();
HibernateUtil.closeSession();
}
However when I try and run this code, I get the following exception:
Quote:
could not initialize proxy - the owning Session was closed
Code:
LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was 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 hibernate.test.Person$$EnhancerByCGLIB$$437a8a09.getEvents(<generated>)
at hibernate.test.EventManager.addPersonToEvent(EventManager.java:96)
Having read the other thread on the subject of this exception, am I right in thinking that the tutorial is wrong ?
Many thanks
Ian Meikle[/code]