-->
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.  [ 5 posts ] 
Author Message
 Post subject: outer join lazy initialization
PostPosted: Fri Apr 29, 2005 2:21 pm 
Newbie

Joined: Fri Apr 29, 2005 2:11 pm
Posts: 2
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.8

Name and version of the database you are using:Oracle 9i

I have the same situtation that was asked in this forum thread: http://forum.hibernate.org/viewtopic.php?t=929721&highlight=one+collection+role+may+fetched+query

shishirksingh wrote:
Hello,

I have 3 tables

A --- one to many --- B ---one to many ---C (they are related by Set collection)

I have the outer join turned off and lazy = true at global level.

For specific instances however, I do need to load all the related collections B and C if I query for A.


However the thread appears to end before the question was answered.

Quote:
How to initialize Collection B and collection C after quering for A (with global lazy = true and outer join = false, using explicit join statements,

without having to do a nested iteration through collection B and initializing C for each B.


And I know about
Quote:
Note that, in the current implementation, only one collection role may be fetched in a query


From other threads it appears that maybe I can achieve this doing 2 queries as opposed to n+1, but I'm not sure how to exaclty this should work. Is there a sample somewhere? Would I execute find twice within the same session, with the two queries? Am I barking up the wrong tree with the two queries, is there another way to avoid the n+1?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 3:05 pm 
Regular
Regular

Joined: Mon Jul 26, 2004 2:28 pm
Posts: 86
Location: Pensacola, Florida
If you have a <many-to-one> reference from B to C, and a <many-to-one> reference from B to A, with outer-join="true", then you could perform a query on C.class to return a list of all C associated with A. That would not initialize A's collection of Bs or B's collection of Cs, but it would give you all of the Cs your are interested in (with references back to B and ultimately A). Depending on how you are using the objects, that may be a quick-and-dirty workaround for the 1+m*n selects problem you are having, but it does have some interesting implications if you are trying to add or remove Bs from A or Cs from B.

You could set outer-join="true" on the <set> of Bs in A or the <set> of Cs in B to change 1+m*n selects to just 1+n or 1+m selects. Which one is up to you, but it would make sense to choose the one that is potentially larger. For example, if the average number of Bs in any given A exceeds the average number of Cs in any given B, then put the outer-join="true" on the <set> of Bs in A. I don't know the particulars, but setting a batch-size on the <class> may also improve performance.

Other than that, I don't know. Depending on your caching strategy you can minimize the number of times that you have to take the 1+m*n hit...if you load up the collections and put them in your user session as detached objects, for example, then you control when they are loaded/refreshed. For example, I have a similar heirarchy with user-role-permission that I load once per user session and keep independent of the Hibernate session or application transactions. It's a significant hit during the log-in process, but that's the only time it hurts.

- Jesse


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 29, 2005 3:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Another (much better) approach is to use HB3 subselect fetching.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 02, 2005 1:45 pm 
Newbie

Joined: Fri Apr 29, 2005 2:11 pm
Posts: 2
gavin wrote:
Another (much better) approach is to use HB3 subselect fetching.


Before I make a jump to ver 3, I'd like to take a look at this. Is there any documentation for this? The doc currently says: TODO

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 9:46 am 
Newbie

Joined: Wed Jan 05, 2005 6:54 am
Posts: 15
davout wrote:
gavin wrote:
Another (much better) approach is to use HB3 subselect fetching.


Before I make a jump to ver 3, I'd like to take a look at this. Is there any documentation for this? The doc currently says: TODO

Thanks.


I follow davout asking for a documentation. The feature is exactly that what I wanted, but it would be interesting to know "how".

Taking a look into the DTD of hibernate3 offers that the fetch attribute of set, bag etc. can take the argument "subselect".

I assume it could be done this way:

[code]
<class name="sample.Entity" table="ENTITY">

<id name="id" type="long">
<column name="ENTITY_ID" not-null="true" />
<generator class="native" />
</id>

<set name="properties" table="ENTITY_PROPERTY" lazy="false" cascade="all-delete-orphan" fetch="subselect">
<key column="ENTITY_ID" />
<many-to-many class="sample.Property" column="PROPERTY_ID" />
</set>
[code]

Am I right?[/b][/code]


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