-->
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.  [ 3 posts ] 
Author Message
 Post subject: OneToOne-Or-Null LeftJoinFetch selection problem
PostPosted: Mon Oct 20, 2008 2:07 am 
Newbie

Joined: Mon Oct 20, 2008 1:34 am
Posts: 6
Hibernate version: 3.2.6.ga

Let me first explain my database layout:
-> There is a table called "TableParent" with a primary key and some columns
-> There is a table called "TableChild" with a primary key, some columns and a foreign key to TableParent, which is unique and not null
-> So there is a one to one-or-null relation between TableParent and TableChild.

My mapping files look like this:

Code:
<hibernate-mapping>
   <class name="TableParent" table="tableParent">
         ...
         <one-to-one name="rowChild" class="TableChild" property-ref="rowParent" fetch="join"/>
         ...      
   </class>
</hibernate-mapping>

<hibernate-mapping>
   <class name="TableChild" table="tableChild">
         ...
         <many-to-one name="rowParent" column="parentId" class="RowParent" not-null="true" unique="true"/>
         ...
   </class>
</hibernate-mapping>


I want to create a query which only selects some of the properties on both tables (and returns null for properties on the child table if there is no child table associated with the parent row) (and i don't want to fetch the child rows together with the parent rows in a single db statement), so my first attempt was to try the following:

Code:
SELECT r.propertyA, r.propertyB, child.propertyA FROM TableParent r LEFT JOIN FETCH r.rowChild child


which does not work because hibernate complains that r itself is not contained in the query... so i added r to the select part of the query (although i don't use it) and then the query can be executed. The problem is that running the query is slow and uses a lot of memory, because hibernate has to create object instances for all returned rows and transfer all the data from the db to the appserver for the selected rows instead of just the properties i want to read (the table contains a lot of columns, hence a lot of data per row)... here is the working query:

Code:
SELECT r.propertyA, r.propertyB, child.propertyA, r FROM TableParent r LEFT JOIN FETCH r.rowChild child


My question is: How can i do a left join fetch without selecting r itself? Is there a way to do so?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 3:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If you change "LEFT JOIN FETCH" to just "LEFT JOIN" I think your original query should work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 3:04 am 
Newbie

Joined: Mon Oct 20, 2008 1:34 am
Posts: 6
yes, that worked :) thanks


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