-->
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.  [ 7 posts ] 
Author Message
 Post subject: Lazy load association not working
PostPosted: Mon Apr 17, 2006 2:23 pm 
Beginner
Beginner

Joined: Thu Dec 01, 2005 12:53 am
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.2

I have following hibernate mapping,

Code:
<hibernate-mapping default-lazy="false">

   <class name="User" table="user">
      <id name="id" column="id">
         <generator class="native" />
      </id>

      <property name="username" column="username" length="120" />
      <property name="lastName" column="last_name" length="60" />
      <property name="firstName" column="first_name" length="60" />
      <property name="emailAddress" column="email_address" length="120" />
      <set name="rights" table="user_right" lazy="true">
         <key column="nimax_user_id" />
         <element column="right_name" type="string" />
      </set>
</hibernate-mapping>


Whenever I load the user object .... the "rights" association gets read always. I have lazy="true" but it seems not working. Can anyone suggest me what to do to make "rights"association lazy load?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 2:35 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
how are you loading it

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 17, 2006 10:48 pm 
Beginner
Beginner

Joined: Thu Dec 01, 2005 12:53 am
Posts: 21
Code:
User user = User.findUserForUsername("super");

Hibernate.getSession().load(User.class, new Long(1));


In both of the above ways .... hibernate does two queries ... one to read the user and other immediatly after it to read its "rights" associtation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 6:55 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
So you're if you are executing both:

Code:

User user = User.findUserForUsername("super");

Hibernate.getSession().load(User.class, new Long(1));


And if the User.findUserForUsername() method executes an HQL, SQL, or Criteria query that returns a user, then Hibernate will executing two queries:

    One to find the user by username
    One to load the user by Id


Session.load() will execute a SQL query to load the entity by identifer.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 1:41 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
lazy=true has been working for me. I don't know about your User.findUserForUsername("super"); without seeing the underlying code, but the session.load has always worked for me. Are you sure that you aren't doing a toString or something else after the load that causes the lazy relationship to be instantiated? Maybe try a println of Hibernate.isInitialized(user.getRights()) right after the load?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 3:22 pm 
Beginner
Beginner

Joined: Thu Dec 01, 2005 12:53 am
Posts: 21
Hi damnhandy,

regarding you post .... you took it the wrong way - I mean to say was ... in any case either through finder which uses Query interface or using the session.load ... the hibernate always reads the "rights" association as another sql query. The lazy="true" on "rights" association seems to be not working for me :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 7:06 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
I see, so either with a query or Session.load(), the assocaition is eagerly loaded. I did notice one thing however, the root of your mapping file specifies:

Code:
<hibernate-mapping default-lazy="false">


The default value for default-lazy is "true", I would suggesst that you remove that attribute from here and use the defaults. I have never set this attribute at the root level so I can't comment on it's behavior, but it would appear that this could overriding the lazy attribute on the rights association?

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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