-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate3 tutorial
PostPosted: Fri Oct 28, 2005 7:47 am 
Newbie

Joined: Fri Oct 28, 2005 7:35 am
Posts: 12
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]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 8:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, map the "event" collection with lazy="false".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 8:02 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Actually, I'm surprised about this piece of code. It was copied from a running application. It's definitely wrong, you also need to use get() instead of load() if you want to avoid the proxy for the aPerson.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 8:17 am 
Newbie

Joined: Fri Oct 28, 2005 7:35 am
Posts: 12
OK, do you have a tutorial that works ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 8:30 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/78.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.