-->
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.  [ 10 posts ] 
Author Message
 Post subject: Now possible to eager fetch multiple associations? [docs]
PostPosted: Tue Apr 18, 2006 10:00 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
at: http://forum.hibernate.org/viewtopic.php?t=952785&highlight=eager it is stated that you can only eagerly fetch a single association.
So:
Code:
                        c.SetFetchMode("relation1", FetchMode.Eager);
                        c.SetFetchMode("relation2", FetchMode.Eager);

Would only actually retrieve one relation.

However, in the documentation (page 93 of the pdf), it says that the above query would actually fetch both associations.

I have a strong feeling though that you can indeed only get one relation. So is the documentation wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 10:36 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
As others (including me) had found...only one list/set/bag can be fetched, but for the number of many-to-one is restricted only bu max-fetch-depth...

AFAIK :)

_________________
Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 18, 2006 2:02 pm 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
Then the documentation is incorrect on this?? it's very confusing.

Also, I don't understand _why_ this is. I understand we can't do multiple joins, but if I want an association to get loaded, it could always do a select afterwards.

Now I have to do a lot more on my DAO..

thnx though!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 3:05 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
If the associated entity is not lazy, then a separate select will be issued right after the main select. Also if the collection has lazy=false, it will also issue a separate select right after. If you want them to be lazy (and probably you want) you can for initialization ussing session.Initialize or issue a new query in order to fetch the associations (yeah, I know it complicates one's life).

I think that a good solution would be to add some new features to query and criteria, smth like:

from a inner join immediate a.collection
or
criteria.SetFetchMode( "collection", FetchMode.Immediate )

Thees two would instruct NH to issue a select immediatly after fetching the result, regardless of "lazy" or "fetch" configuration in mappings.

PS: What would you say sergey :) ?

_________________
Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 3:13 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
i'm with you on this one.. Although I thought that Eager would mean exactly that.

In my scenario the relation ship is mapped lazy, but the dao gets an enumeration that states the fetchpath.. So in the specific case, the user needs the association fetched and ready to go.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 3:25 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
wow, i'm sorry dado, but I'm still not getting it exactly..

I am fetching a list of objects.. each of these objects have an association that needs to be fetched. Does this mean that the best practice here is to iterate the list and call nhibernateutil.initialize ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 1:38 pm 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Hmm, in your case I think that the best thing is to re-query for the needed data.

Document d = new Criteria(typeof(Document))
// here get all needed many-to-one associations
.SetFetchMode( "Author", FetchMode.Join )
.SetFetchMode( "Category", FetchMode.Join )
// here get the collection (in rare cases I need more than a collection though)
.SetFetchMode( "Pages", FetchMode.Join )
.Add(Expression.Like("Name", "John"));

// perform a new criteria for the other associations
d = new Criteria(typeof(Document))
.SetFetchMode( "Readers", FetchMode.Join )
.Add(Expression.Like("Name", "John"));

this will result in two selects...

About the initialize, I think I was mistaken as it will only initialize the given argument (not the elements just the collection)..

What do you think?

_________________
Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 9:22 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
I've been thinking about it, but I don't understand why doing it this way is more efficient then doing a nhibernateutil.initialize.
In your solution you probably have to attach the association yourself, is that right?

the initialize should result in only one extra select as well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 3:37 am 
Regular
Regular

Joined: Tue Mar 15, 2005 12:38 pm
Posts: 73
Location: Bucharest
Well, calling session.Initialize on N objects will result in N selects. Moreover, calling session.Initialize in a many-to-many association will not initialize the collection content AFAIK if the elements are lazy (will only get the list of ID's from the DB). In contrast, issuing another query will FetchMode.Join will issue one select for all N objects...

Correct ?

_________________
Dragos


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 5:09 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
that would be awesome.. I will try to look into this when i get the time (Usecases getting in the way ;-( )


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