-->
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.  [ 3 posts ] 
Author Message
 Post subject: lazy="false" property is not working, still lazy
PostPosted: Tue Apr 03, 2012 2:28 am 
Newbie

Joined: Sun Dec 30, 2007 1:08 pm
Posts: 14
Hi,
it seems as if the lazy="false" property on a set mapping is not working in my mapping file.
I want to eager load a one-to-many relationship. I thought it would be enough to just set the lazy property to false.
Nevertheless I get a LazyInitializationException when I want to access the relationship set after the session is closed.

Any ideas what I do wrong? Do I really need to do an explicit left join fetch to eager load a relationship?

Hibernate-Version: 3.2.5.ga
Database: HSQL DB

Here my mapping file:
Code:
<hibernate-mapping >
    <class name="crud.book.model.Publisher" table="PUBLISHER" schema="PUBLIC">
        <id name="id" type="int">
            <column name="ID" />

            <generator class="assigned" />
        </id>

        <property name="name" type="string">
            <column name="NAME" length="20" not-null="true" />
        </property>

        <set name="books" inverse="true" lazy="false" cascade="all" fetch="subselect">
            <key>
                <column name="PUBLISHER" />
            </key>

            <one-to-many class="crud.book.model.Book" />
        </set>
    </class>
</hibernate-mapping>


The query code:
Code:

SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
      Session session = sessionFactory.getCurrentSession();
      org.hibernate.Transaction tx = session.beginTransaction();

      try {
         session = sessionFactory.getCurrentSession();
         tx = session.beginTransaction();

                       Publisher loadedPublisher = (Publisher) session.load(Publisher.class, 1);

                        tx.commit();

                        //--> getting a LazyInitializationException here
         Set<Book> books = loadedPublisher.getBooks();

                } catch (Exception e) {
         ...
      } finally {
         ...
      }


Top
 Profile  
 
 Post subject: Re: lazy="false" property is not working, still lazy
PostPosted: Tue Apr 03, 2012 2:45 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is the Publisher object that is lazy-loaded. The Session.load() method will only give you an uninitialized proxy and thus you get an LazyInitializationException when accessing a method on the Publisher object after closing the session. You could try using Session.get() instead or make sure the proxy is initialized by calling Hibernate.initialize(loadedPublisher).


Top
 Profile  
 
 Post subject: Re: lazy="false" property is not working, still lazy
PostPosted: Tue Apr 03, 2012 3:42 pm 
Newbie

Joined: Sun Dec 30, 2007 1:08 pm
Posts: 14
Hi,
thank you for your quick answer.

You are right with the difference in session.load(...) vs session.get(...).
It solves the LazyInitializationException on the Publisher object.

But nevertheless an exception is thrown when I access the book relationship.

I have changed the code to:

Code:
...
Publisher loadedPublisher = (Publisher) session.get(Publisher.class, publisherId);
Hibernate.initialize(loadedPublisher);
tx.commit();
Set<Book> books = loadedPublisher.getBooks();


And the Exception is:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: crud.book.model.Publisher.books, no session or session was closed

Any ideas?
Again, I can solve this by doing an explicit left join fetch on the relationship property, but what is the lazy attribute in mapping file for if it changes nothing in the behavior of the framework?


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