-->
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.  [ 7 posts ] 
Author Message
 Post subject: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Fri Feb 05, 2010 5:09 am 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I have a hierarchy of classes mapped to a single table, with an integer discriminator column. In this situation is it possible to include a test of the class type in a HQL where clause?

For example, take a class mapping that has a common field called 'target' and the following class hierarchy....

Code:
     BaseClass
         |---SubClass1
         |---SubClass2

.... how would I create a HQL query that equates to ....
Code:
from BaseClass as m where (m.target = 1 and m.class = subClass1) or (m.target = 3 and m.class = subClass2)


Any suggestions?


Top
 Profile  
 
 Post subject: Re: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Fri Feb 05, 2010 12:30 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi davout_uk,

hm, I guess this is only possible if you'd be using content-based discrimination. Then you would have access to the discriminator in queries. Otherwise, I'm afraid the discriminator cannot be used from outside.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Thu Feb 11, 2010 12:23 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
I found a way of mapping to the discriminator, by adding a new read only property, as shown below...

Code:
<discriminator column="ActionDiscriminator" type="java.lang.Integer" />
<property name="typeID" insert="false" update="false" type="java.lang.Integer">
      <column name="ActionDiscriminator" />
</property>


Top
 Profile  
 
 Post subject: Re: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Thu Feb 11, 2010 12:41 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi davout_uk,

congrats on the solution. If it works - fine. This is very close to content-based discrimination as I suggested. I personally think, content-based discrimination is more elegant.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Thu Feb 11, 2010 12:48 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 1:04 pm
Posts: 26
You can use query like this:
Code:
from Cat cat where cat.class = DomesticCat

Found it in the documentation
Then you request wille be :
Code:
from BaseClass as m where (m.target = 1 and m.class = SubClass1) or (m.target = 3 and m.class = SubClass2)


Top
 Profile  
 
 Post subject: Re: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Thu Feb 11, 2010 12:57 pm 
Regular
Regular

Joined: Tue Apr 10, 2007 10:02 am
Posts: 56
What do you mean by "content-based discrimination"? I can't see anything listed on this topic in the link you provided.

Froestel wrote:
Hi davout_uk,

congrats on the solution. If it works - fine. This is very close to content-based discrimination as I suggested. I personally think, content-based discrimination is more elegant.

CU
Froestel


Top
 Profile  
 
 Post subject: Re: Class hierarchy: how to HQL test class type in where clause?
PostPosted: Thu Feb 11, 2010 1:52 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi

Hibernate online documentation, see Section 23.4.4 Content based discrimination.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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