-->
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.  [ 12 posts ] 
Author Message
 Post subject: BiDirectional Association-lazyloading and immediate fetching
PostPosted: Tue Aug 23, 2005 2:41 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5



I have a general question. I am working on a many-to-one association. I want the many side to be fetched immediately while if i move from the one side to the many side, i need the other object to be lazy-loaded?

Basically A [1] <---> B[0..*]

When i fetch an instance of A i want instances of B also to be fetched immediately. When i fetch an instance of B, i want instances of A to only be loaded lazily?

I have tested the sample with lazy="true" but it doesn't work that way. It loads both the instances of A and B even though i just requesed an instance of B.


Is this possible that a single bidirectional association can have both the features enabled, of lazy loading and immediate fetching?

Thanks,
Surya


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 2:57 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
I don't understand very much your doubt.
If you want to access the one part of the many-to-one immediately, you should inform that lazy is false.
Code:
<many-to-one name="an_object" column="SO_ID" unique="true" not-null="true" lazy="false" />

You should put lazy="false" in the mapping of collection, if you want fetch the collection immediately.
This answer your doubt?


Top
 Profile  
 
 Post subject: BiDirectional Association-lazyloading and immediate fetching
PostPosted: Tue Aug 23, 2005 3:18 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
ronaldorezende wrote:
I don't understand very much your doubt.
If you want to access the one part of the many-to-one immediately, you should inform that lazy is false.
Code:
<many-to-one name="an_object" column="SO_ID" unique="true" not-null="true" lazy="false" />

You should put lazy="false" in the mapping of collection, if you want fetch the collection immediately.
This answer your doubt?


I understand that setting lazy="false" will do immediate fetching.

My question is can i set lazy="false" on the one part and lazy="true" on the many part for the same associated link.
Meaning in one mapping file i will have this following many-to-one link.

<many-to-one name="an_object" column="SO_ID" unique="true" not-null="true" lazy="true" />

The other associated mapping file will have a collection property.

for example:
<set name="another_object" lazy="false">
<key column="SO_ID" not-null ="true"/>
<one-to-many class="class_name where many-to-one link resides"/>
</set>

Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 3:23 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Yes, I think so. Put your code and your mappings here. Maybe there is something wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 3:51 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
ronaldorezende wrote:
Yes, I think so. Put your code and your mappings here. Maybe there is something wrong.


Mapping files

a) Net.hbm.xml mapping file [One part]

<hibernate-mapping>
...
<set name="imdscannedconfig" lazy="false"
inverse="true"
cascade="save-update">
<key column="NET_ID"></key>
<one-to-many class="com.hibernate.example.pos.Imdscannedconfig"/>
</set>
...
</hibernate-mapping>

b) Imdscannedconfig.hbm.xml mapping file [Many part]
<hibernate-mapping>
....

<many-to-one name="net"
column="NET_ID"
class="com.hibernate.example.pos.Net"
lazy="true"
not-null="true"/>
..
</hibernate-mapping>


Code

Imdscannedconfig testImdscann = new Imdscannedconfig()
List canScanList = sess.createCriteria(testImdscann.getClass()).list();


This does one query to get Imdscannedconfig object and its other properties but then it does a 2nd query to go and get that "net" objects as well while i want that net objects to be loaded lazily.

Thanks,
Surya


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 4:32 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
Surya,
I think that there is a bug on hibernate. Let the many-to-one without any lazy attribute (proxy, the default).
I did a test here and observed a strange result.

If I put this:
<many-to-one name="cliente" column="CLIENTE_ID" not-null="true"/>
The cliente isn't fetched.

<many-to-one name="cliente" column="CLIENTE_ID" lazy="false" not-null="true"/>
The cliente isn't fetched.

<many-to-one name="cliente" column="CLIENTE_ID" lazy="true" not-null="true"/>
The cliente is fetched.

This is an bidirectional many-to-one association.

Code:
           <bag name="orcamentos" lazy="true" inverse="true" cascade="save-update">
                   <key column="CLIENTE_ID" not-null="true" />
                   <one-to-many class="src.model.Orcamento" />
                </bag>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 4:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Isn't it amazing how many "bugs" are fixed when you actually read documentation for things you are using:

http://www.hibernate.org/hib_docs/v3/re ... -manytoone

Quote:
lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="true" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation).


We have renamed this option in 3.1 because so many users are allergic to that pesky documentation stuff we put so much time and effort into.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 4:48 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
It isn't a bug.
Take a look at this link:
http://opensource2.atlassian.com/projec ... se/HHH-862


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 5:57 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
gavin wrote:
Isn't it amazing how many "bugs" are fixed when you actually read documentation for things you are using:

http://www.hibernate.org/hib_docs/v3/re ... -manytoone

Quote:
lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="true" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation).


We have renamed this option in 3.1 because so many users are allergic to that pesky documentation stuff we put so much time and effort into.


My Question still remains.
I understood the part that as and when i access the instance variable first is when lazy="true" works.

this is my scenario.

Net.hbm.xml file.
<hibernate-mapping>
<class name="com.hibernate.example.pos.Net" table="NETS">
.....
<!-- corresponds to bidirecional one-to-many -->
<set name="imdscannedconfig" lazy="true"
inverse="true"
cascade="save-update">
<key column="NET_ID"></key>
<one-to-many class="com.hibernate.example.pos.Imdscannedconfig"/>
</set>
</class>
</hibernate-mapping>

Imdscannedconfig.hbm.xml
<hibernate-mapping>
<class name="com.hibernate.example.pos.Imdscannedconfig" table="IMDSCANNED">
....
<many-to-one name="net" lazy="true"
column="NET_ID"
class="com.hibernate.example.pos.Net"
not-null="true"/>
...
</class>
</hibernate-mapping>


When i do a

session.load(Net.class, id).
The lazy="true" parameter on the many-to-one tag works and doesn't select the Imdscannedconfig object till i first access the instance variable.
Basically till i do a
NetObject.getImdscannedconfig(). it doesn't do a second load.

When i do a
Imdscannedconfig testimds = new Imdscannedconfig();
List lstImds = session.createCriteria(testimds.getClass()).list();

It does two SQL queries to load the Imdscannedconfig object first and then does a second query to load the Net Object as well even though i have not accessed the Instance Variable testimds. I need to avoid this second query.

When i load a Imdscannedconfig object, i don't need to load the Net Object.
Net[1] <-> Imdscannedconfig[0..*]

When i load a net object, i want the Imdscannedconfig object to be loaded when i call the getter method, which when i set Lazy="true" on the Set object works.

But when i load a imdscannedconfig, i don't want it load the net object. Can this be done?
I hope my question is clear now.

Thanks,
Surya


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 7:15 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 2:15 pm
Posts: 66
As I said before, just remove lazy="true" from your many-to-one mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 9:51 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
ronaldorezende wrote:
As I said before, just remove lazy="true" from your many-to-one mapping.


Yes, That did it.

Thanks for the help guys!!


Top
 Profile  
 
 Post subject: The documentation needs a bit of work
PostPosted: Thu Aug 25, 2005 9:43 am 
Newbie

Joined: Tue Aug 23, 2005 11:14 am
Posts: 5
Location: London
This may have been fixed by the documentation, but I have found little useful how-to or tutorial code. I hope the APIs are reviewed by people with no understanding of how the code works internally. I would like to see more working examples, specifically covering all of the uses of collections, and demonstrating the effect of all those pesky attributes. Them thar varmints deserve some attention.

Michael



gavin wrote:
Isn't it amazing how many "bugs" are fixed when you actually read documentation for things you are using:

http://www.hibernate.org/hib_docs/v3/re ... -manytoone

Quote:
lazy (optional - defaults to proxy): By default, single point associations are proxied. lazy="true" specifies that the property should be fetched lazily when the instance variable is first accessed (requires build-time bytecode instrumentation).


We have renamed this option in 3.1 because so many users are allergic to that pesky documentation stuff we put so much time and effort into.


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