-->
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.  [ 1 post ] 
Author Message
 Post subject: HQL query for OUTER JOIN between superclass and subclass ?
PostPosted: Mon Feb 02, 2009 9:19 am 
Newbie

Joined: Wed Jun 18, 2008 1:43 am
Posts: 3
Hi,

I need to write a HQL query. The HQL query mainly contains OUTER JOIN between superclass and subclass. Because we want to get property belonging to both superclass as well subclass. The superclass and subclass relation have been implemented as "table per hierarchy".


The relationship between objects are something like this:

Object1 => a separate class/table
<class name="Object1" table="Object1">
<property name="property1" column="property1_col" />""
<many-to-one name="superclass" column="superclass_id" not-null="true" />
</class>


SuperClass, SubClass1 => these are implemented as "table per class hierarchy"
<class name="SuperClass" table="SuperClass">
<property name="property2" column="property2_col" />

<subclass name="SubClass1" discriminator-value="SubClass1">
<many-to-one name="sub-property">
<column name="sub_property_id" index="" />
</many-to-one>
</subclass>

<subclass name="SubClass2" discriminator-value="SubClass2">
</subclass>
</class>


Now I've to write an HQL query equal to following SQL query:

SELECT object1Item.property1, superclassItem.property2, superclassItem.sub_property_id
FROM Object1 as object1Item, SuperClass as superclassItem
WHERE object1Item.superclass_id = superclassItem.id and object1Item.property1 like '%test%'

(Here we have to take all the rows from SuperClass table including rows pointing to SubClass1. That means SuperClass rows belonging to some other subclasses should also be taken into account (or) we can say OUTER JOIN between SuperClass and SubClass1)

I was working on possible HQL queries equivalent to above SQL one that could be used to list the cellRule fields along with SKU name for Redemption cell rules only. Some of them are as follows:

1)
SELECT object1Item.property1, superclassItem.property2 , superclassItem. sub-property
FROM Object1 as object1Item, SuperClass as superclassItem
WHERE object1Item.superclass = superclassItem and object1Item. property1 like '%test%'

== Hibernate does not accept this query and gives an error message saying "sub-property is not a property of SuperClass class". Hibernate considers "sub-property" as property of 'SubClass1' subclass instead of Superclass.


2)
SELECT object1Item.property1, subclassItem.property2 , subclassItem. sub-property
FROM Object1 as object1Item, SubClass as subclassItem
WHERE object1Item.superclass = subclassItem and object1Item. property1 like '%test%'

== This HQL query will include only SubClass rows and thus does not give all result rows.


3)
SELECT object1Item.property1, superclassItem.property2 , subclassItem. sub-property
FROM Object1 as object1Item, SuperClass as superclassItem, SubClass as subclassItem
WHERE object1Item.superclass = superclassItem and object1Item. property1 like '%test%'

== This HQL query gives all the result rows but is inefficient from performance point of view as we are including SuperClass table twice without any join condition.


Plz give me your feedback on this.
Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.