-->
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.  [ 6 posts ] 
Author Message
 Post subject: Lazy Loading with many-to-one
PostPosted: Tue Jul 06, 2010 8:19 am 
Newbie

Joined: Tue Jul 06, 2010 7:56 am
Posts: 4
I have 2 Beans with a many-to-one association connecting them: an Order and a Customer.
Both Beans are configured with lazy loading = false.
The Association is configured with lazy loading = proxy.

Code:
<hibernate-mapping>
  <class lazy="false" name="Order" table="Salesorder" optimistic-lock="version">
  ...
  <many-to-one name="customer"
       class="Customer"
       column="customer_id"
       not-null="true"
       lazy="proxy"/>
  ...


Code:
<hibernate-mapping>
  <class lazy="false" name="Customer" table="Customer" optimistic-lock="version">
  ...


In this scenario Hibernate ignores the lazy configuration for the association while loading an Order: the Customer record is fetched eagerly.
The lazy configuration for the Bean seems to override the lazy configuration for the association.
Is this assumption true? I didn't find a hint in the documentation.
Can I change this behaviour?

Greetings,
Takashi


Top
 Profile  
 
 Post subject: Re: Lazy Loading with many-to-one
PostPosted: Tue Jul 06, 2010 10:13 am 
Newbie

Joined: Tue Jul 06, 2010 7:56 am
Posts: 4
I have tested my lazy loading settings with an one-to-many association and in contrast to the many-to-one association the lazy attribute for the association is evaluated.
Here are my code snippets for two Beans, Order and Orderposition:

Code:
<hibernate-mapping>
  <class lazy="false" name="Order" table="Salesorder" optimistic-lock="version">
  ...
  <set name="positions" inverse="true" lazy="true" >
    <key column="salesorder_id" not-null="true"/>
    <one-to-many class="Orderposition"/>
  </set>
  ...


Code:
<hibernate-mapping>
  <class lazy="false" name="Orderposition" table="Orderposition" optimistic-lock="version">
  ...


With these settings the Orderpositions are really lazy loaded when loading the Order bean. Even though the Orderposition bean itself is configured with lazy = false.
Maybe this inconsistency to the many-to-one association is a Bug in Hibernate?
I have tested this behaviour with Hibernate 3.5.0 and 3.5.3.


Top
 Profile  
 
 Post subject: Re: Lazy Loading with many-to-one
PostPosted: Tue Jul 06, 2010 12:44 pm 
Newbie

Joined: Tue Jul 06, 2010 11:37 am
Posts: 6
Hi takashi,
didnt you get any error by giving lazy="proxy"??? for many to one.... i didnt try for associations up 2 now....please reply me for your way of doing.......

as per i know........the way of doing is......

there is no reason to brig in the entire collection when u only want to look at a few elements.you can help hibernate by specifying the proxy attribute in the <class> or <sub-class> element ,defining the class that should be used for lazy initialization.the class is the same as the class being mapped.

example :

<class name ="Group" proxy="Group">
<set lazy="true">
</set>
</class>


please reply after solving it.......

Thanks,
M.Rakesh


Top
 Profile  
 
 Post subject: Re: Lazy Loading with many-to-one
PostPosted: Tue Jul 06, 2010 3:28 pm 
Newbie

Joined: Tue Jul 06, 2010 7:56 am
Posts: 4
Thx for your answer!

Quote:
didnt you get any error by giving lazy="proxy"???

Actually lazy="proxy" is the default value for this many-to-one attribute. Other possible values are "false" and "no-proxy".

As i have written in my second posting the lazy attribute for the <set> works fine for me.
1) <set name="..." lazy="true"> the collection is lazy loaded as expected.
2) <set name="..." lazy="false"> the collection is fetched eagerly.

The <many-to-one> makes my confusion.
1) <many-to-one name="..." lazy="proxy"/> the object is fetched eagerly. <<<<<Thats the problem
2) <many-to-one name="..." lazy="false"/> fetched eagerly too, as you expected.


Top
 Profile  
 
 Post subject: Re: Lazy Loading with many-to-one
PostPosted: Thu Jul 08, 2010 11:52 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
To lazy load a many to one the class on the one side also has to be marked as lazy as it needs to be able to be proxied.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: Re: Lazy Loading with many-to-one
PostPosted: Thu Jul 08, 2010 12:48 pm 
Newbie

Joined: Tue Jul 06, 2010 7:56 am
Posts: 4
Thx for your answer!

I suppose the collection object used for the ony-to-many association is proxied in contrast to the object on the one side in the many-to-one association. This is the major difference to many-to-one. It seems to be inconsistent for me. Where would I improve the documentation to give other users a hint for this behaviour? I think it's an important aspect of associations.


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