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.  [ 11 posts ] 
Author Message
 Post subject: detached objects and LazyInitializationException
PostPosted: Fri Jul 22, 2005 9:13 am 
Newbie

Joined: Tue Jul 19, 2005 8:58 am
Posts: 6
I'm having problems with detached objects and LazyInitializationException on a Set with Hibernate 3. After having read through Hibernate in Action and tried every possibly combination of metadata, changing hibernate.max_fetch_depth, adding Hibernate.initialize( ) before closing the session etc, I started searching the web for more information on working with detached objects.

On the web, I found several suggestions for workarounds: like saving the session for the whole web request or using the Spring OpenSessionInViewFilter, but not much on how lazy loading works. I found at least two blogs that suggested not closing the session for the duration of a request, just to avoid getting a LazyInitializationException.

Does anybody know of a good explanation of how to get an object graph loaded (other than the HIA book)?

Here is the code that is giving me a headache, with class names changed to the HIA book example:

Code:
   <class name="Item" table="items">
                ...
      <set name="bids" inverse="true" lazy="false" >
         <key column="ITEM_ID" />
         <one-to-many class="Bid" />
      </set>
      .... a bunch of properties...
   </class>

   <class name="Bid" table="bids">
                ...
      <many-to-one name="item" column="ITEM_ID" class="Item"       cascade="save-update"/>
      .... a bunch of properties, sets and many-to-one mappings ...
   </class>



The following code gives a LazyInitializationException:

Code:

        Session session = sessionFactory.openSession();
        Item item = (Item) session.get(Item.class, id);
        Hibernate.initialize(item.getBids());
        session.close();

        item.getBids(); --> throws LazyInitializationException


I have tried with criteria.setFetchMode("bids", FetchMode.EAGER), without success.

Jonas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 9:16 am 
Hibernate Team
Hibernate Team

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 11:54 am 
Newbie

Joined: Tue May 25, 2004 11:42 am
Posts: 10
Location: Oxford, England
I've got a similar problem to Jonas and I am wondering if it is a bug in Hibernate 3. The lazy attribute for sets seems to be being ignored or at least not being set up correctly.

If I load an object containing a set, close the hibernate session and then try to add an object to the set I get an
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

thrown.

This seems wrong to me. If I explictely access the set before closing the session then I get the correct result. I would have thought that setting the lazy attribute to false would have the same effect as accessing the set in the session but it doesn't appear to.

I'm using version 3.0.5 of Hibernate with Java 1.5.0_02.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I've got a similar problem to Jonas and I am wondering if it is a bug in Hibernate 3. The lazy attribute for sets seems to be being ignored or at least not being set up correctly.


You are mistaken.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:21 pm 
Newbie

Joined: Tue May 25, 2004 11:42 am
Posts: 10
Location: Oxford, England
OK - so I am mistaken in my assumptions about what lazy does.

How then do I guarantee that an object (containing a set) retrieved from the database is fully populated before I close the session?

I need to be able to do this so I can safely send the object to the UI layer in my application. I know I could do this by explicitly accessing the set after I retrieve it but that doesn't seem like a great thing to have to do.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
<set lazy="false"> will guarantee this for a collection

<many-to-one lazy="false"> will guarantee this for a single-point association

get() will guarantee this for an entity instance just retrieved


Of course, an entity instance being available does not guarantee that its collections are available.

(Your confusion is that you don't know the difference between "a proxy" is not fetched and "a collection" is not fetched. Read error messages more carefully.)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:40 pm 
Newbie

Joined: Tue May 25, 2004 11:42 am
Posts: 10
Location: Oxford, England
It doesn't seem to for me.

Here's a snippet of the config file

Code:
   <class name="aaaa.core.data.User" table="user" select-before-update="true" >
      <id name="id" type="int" column="userId" unsaved-value="0">
         <generator class="native"/>
      </id>
      <property name="location" column="location" type="string"/>
....
        <set name="aliases" cascade="all" table="alias" sort="natural" lazy="false" >
            <key column="primaryUserID"/>
            <element column="emailAddr" type="string"/>
        </set>
   </class>


and here is some code that fails.
Code:

User user = null;
        Session session = null;
        try {
            session = sessionFactory.openSession();
            user = session.load(User.class, new Integer(userId));
            session.flush();
            session.disconnect();
            session.close();
        } catch (HibernateException e) {
            s_log.error("UserFarm Exception", e);
            throw new FarmException("Failed to retrieve user - " + userId, e);
        }
        user.addAlias("aaaaaaaa");


the final line throws the Proxy initialization exception.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:43 pm 
Newbie

Joined: Tue May 25, 2004 11:42 am
Posts: 10
Location: Oxford, England
The user.addAlias() method is

Code:
    public void addAlias(String alias) {
        this.aliases.add(alias);
    }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Did you even read what I wrote?

It is a "proxy" that is uninitialized. Why? Because you use load() instead of get().


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:06 pm 
Newbie

Joined: Tue May 25, 2004 11:42 am
Posts: 10
Location: Oxford, England
Thanks for that Gavin. It all starts to make a bit more sense now.

cheers.


Top
 Profile  
 
 Post subject: Found my problem, but still...
PostPosted: Fri Jul 22, 2005 5:31 pm 
Newbie

Joined: Tue Jul 19, 2005 8:58 am
Posts: 6
Ask a question, and the answer immediately pops up in your head. In the end, my problem had nothing to do with lazy loading. I had added a method to set associations in both directions, inatvertently calling it setBid causing org.hibernate.LazyInitializationException: cannot access loading collection. I was thrown off by the fact that it was wrapped as a LazyInitializationException.

As a newcomer to Hibernate, the question of what is loaded when is by far the most confusing. The HIA book is extremely clear in describing most topics like mapping associations, HQL etc. I am still rather confused about what is loaded when, however. I looked through http://www.hibernate.org/37.html
before posting, finding only some partial explanations in http://www.hibernate.org/162.html. Part of my confusion is probably due to changes in defaults and naming between Hibernate 2 and 3. Time to start experimenting...

Jonas


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