-->
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: Outer join fetching in parent child (1--*) relationship
PostPosted: Tue Feb 10, 2004 7:28 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Hi,

I have a simple Parent child relationship. The mappings are as follows:
Parent.hbm.xml
Code:
<hibernate-mapping>
<class name="eg.Parent" table="parent">   
   <id name="id" column="id" type="integer" unsaved-value="null" >
      <generator class="native"/>
   </id>   
   <property name="name" not-null="true" column="name" />   
      
   <set name="children" cascade="all" inverse="true" outer-join="true">
      <key column="parent_id"/>
      <one-to-many class="eg.Child" />
   </set>      
</class>
</hibernate-mapping>

and Child.hbm.xml
Code:
<class name="eg.Child" table="child">   
   <id name="id" column="id" type="integer" unsaved-value="null" >
      <generator class="native"/>
   </id>   
   <property name="name" not-null="true" column="name" />
   
   <many-to-one name="parent" class="eg.Parent" column="parent_id" not-null="true" outer-join="true" />
</class>

</hibernate-mapping>

As you can see, the set is mapped with outer-join=true. This is a feature that I want to take advantange of.
I also have the following properties set in my hibernate.cfg.xml file:
Code:
<property name="use_outer_join">true</property>
<property name="max_fetch_depth">3</property>


When I do the a simple load on the Parent, the outer join fetching works. i.e
Code:
Parent parent = (Parent)session.load(Parent.class, id);

Hibernate: select parent0_.id as id1_, parent0_.name as name1_, children1_.id as id__, children1_.parent_id as parent_id__, children1_.id as id0_, children1_.name as name0_, children1_.parent_id as parent_id0_ from parent parent0_ left outer join child children1_ on parent0_.id=children1_.parent_id where parent0_.id=?


However when I do a find, I get the following
Code:
List parents = session.find("from Parent p");
(or)
List parents = session.createQuery("from Parent p").list();

Hibernate: select parent0_.id as id, parent0_.name as name from parent parent0_
Hibernate: select children0_.id as id__, children0_.parent_id as parent_id__, children0_.id as id0_, children0_.name as name0_, children0_.parent_id as parent_id0_ from child children0_ where children0_.parent_id=?

i.e. Two queries with no outer join. To take advantage of the outer join facility I have to explicitly fetch the children. i.e.
Code:
List parents = session.find("from Parent p left join fetch p.children");
(or)
List parents = session.createQuery("from Parent p left join fetch p.children").list( );

.. which results in an outer join (as expected).

From the docs:
Quote:
Outer join fetching allows a graph of objects connected by many-to-one, one-to-many or one-to-one associations to be retrieved in a single SQL SELECT.

and goes on to say that:
Quote:
Only one collection may be fetched by outer join per SQL SELECT.


My question is:
Why - if you have mapped the collection as non-lazy (default) and outer-join="true" - is the collection not fetched in an outer-join manner when using find() and createQuery() if only one such 'outer-join' collection exists?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 10:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Basically, because HQL is built on top of SQL and mostly respect the SQL "expected" behavior.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 11:38 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
emmanuel wrote:
Basically, because HQL is built on top of SQL and mostly respect the SQL "expected" behavior.


So what you are telling me is that for the case of a parent-child relationship mapped in a 1 -- * manner, outer join fetching for the collection will only be used (as defined in the hibernate mapping) by:
- session.load(Class clazz, Serializable id) on the parent class.
- when navigating to a persistent parent object reference (with the session open obviously)
if
outer-join ="true" on the collection.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 1:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you experienced that, I would say yes except for the
Quote:
if outer-join="true"
.
It should be the same behavior when outer-join="auto" and the other side isn't lazy or proxied.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 2:12 pm 
Beginner
Beginner

Joined: Thu Oct 09, 2003 3:42 pm
Posts: 22
I found these two posts helpful in understanding outer join behavior

http://forum.hibernate.org/viewtopic.php?t=924674

http://forum.hibernate.org/viewtopic.php?t=219


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:43 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Thank you. The posts you mentioned did help to clarify things.


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.