-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL Problem using a left join in a simple subquery
PostPosted: Mon Jun 09, 2008 12:53 am 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
I am not sure how to do a left outer join in a HQL subquery

Code:
from ParentObj parentObj left join parentObj.childCollection as children
    where children.anotherParentObj.anotherParentField != 1


generates a SQL

Code:
SELECT parent.parent_id, childTable.parent_id
  FROM ParentTable parent, ChildTable childTable, AnotherParent anotherParent
WHERE parent.parent_id = childTable.parent_id(+)
   AND anotherParent.anotherParent_id != 1


whereas I am actually interested in this one... where I filter my child based on another parent and then do a left join on this child with the base parent!

Code:
SELECT parent.parent_id, childAlias.parent_id
  FROM ParentTable parent,
       (SELECT *
          FROM ChildTable childTable, AnotherParent anotherParent
         WHERE childTable.anotherParent_id = anotherParent.anotherParent_id
         AND anotherParent.anotherParent_id != 1) childAlias
WHERE parent.parent_id = childAlias.parent_id(+)


Another question would be how they would do a criteria query for this!

Thanks
Krishna


Top
 Profile  
 
 Post subject: Fixed the left join subquery issue
PostPosted: Mon Jun 09, 2008 6:23 pm 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
Solved the problem by changing my query as:

Code:
from ParentObj parentObj left join fetch parentObj.children as childObj
left join fetch parentObj.anotherParent1
left join fetch parentObj.anotherParent2 
where parentObj.parentId not in 
(select tmpChildObj.parentObj.parentId from 
ChildObj tmpChildObj where tmpChildObj.someOtherParent.someOtherParentId = ? )
.concat(dynamicWhereClauseQuery).concat(" order by parentObj.parent_id ASC");


This uses both left join with eager fetching (by saying left join fetch) on parent, child and also filters the child objects based on some other parent.

I am still wondering how this can be written neatly in a criteria query!

-Cheers
Krishna


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