-->
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.  [ 9 posts ] 
Author Message
 Post subject: Query Question
PostPosted: Wed Aug 03, 2005 7:03 pm 
Newbie

Joined: Wed Aug 03, 2005 6:49 pm
Posts: 9
Here is a short desc of how our domain model looks like.

There is a unidirectional one-to-many mapping between A and B.

B has subclasses C and D

A has no knowledge of C and D. It is only aware of B by the mapping.

Abstract superclass B and its subclasses C and D are mapped via table per hierarchy.

C has a mapped property getPropertyFromC() for instance
D has a mapped property getPropertyFromD() for instance

How can I issue a query (via hql or criteria api) to find the list of A objects which have (C objects where PropertyFromC is ValueC) or (D objects where PropertyFromD is ValueD)

Seems like I need to do type casting or something in hql which is not possible.

Any suggestions ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 6:18 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
I don't know, if there's a more elegant solution, but I think that will work.

Code:
SELECT c.id FROM C AS c
WHERE c.propertyFromC = :valueC


Code:
SELECT d.id FROM D AS d
WHERE d.propertyFromD = :valueD


Now combine both ID lists to one list. Each ID must be unique since you are using table per class hierarchy.

Code:
SELECT a FROM A AS a
JOIN a.bCollection AS b
WHERE b.id IN (:idList)


Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 8:27 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
define getPropertyFromC and getPropertyFromD in B (override in C and D)

then do query :

from A a
where (a.discriminator='C' and b.propertyFromC='valueC')
or (a.discriminator='D' and b.propertyFromD='valueD')

regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 8:33 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Code:
from A a
where (a.discriminator='C' and b.propertyFromC='valueC')
or (a.discriminator='D' and b.propertyFromD='valueD')


I'm sorry - this is correct
Code:
from A a
where (a.discriminator='C' and a.b.propertyFromC='valueC')
or (a.discriminator='D' and a.b.propertyFromD='valueD')
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 04, 2005 8:47 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
snpesnpe wrote:
I'm sorry - this is correct
Code:
from A a
where (a.discriminator='C' and a.b.propertyFromC='valueC')
or (a.discriminator='D' and a.b.propertyFromD='valueD')


Yes, that's another solution, but it would be:

Code:
from A a
where (a.b.discriminator = :cDiscriminator and a.b.propertyFromC = :valueC)
or (a.b.discriminator =  :dDiscriminator and a.b.propertyFromD = :valueD)

The discriminator is placed in B.
But that was probably a slip and it is just what you meant.

Best regards
Sven


Top
 Profile  
 
 Post subject: Thx
PostPosted: Sat Aug 06, 2005 6:08 am 
Newbie

Joined: Wed Aug 03, 2005 6:49 pm
Posts: 9
Thanks for all the replies.
Is there a way to do this in one query only without using native sql?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 6:22 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
The last one is one query and not native SQL. :)


Top
 Profile  
 
 Post subject: Ans.
PostPosted: Sat Aug 06, 2005 7:12 am 
Newbie

Joined: Wed Aug 03, 2005 6:49 pm
Posts: 9
But the problem with that is I will need to carry propertyC and propertyD to the abstract class where they will not make any sense except making the query work. Can do that as a workaround but looking for a permanent solution for such problems.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 12:39 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Code:
SELECT a FROM A a JOIN a.bCollection bElement
WHERE
bElement in (SELECT c FROM C c where c.propertyFromC='value')
OR
bElement in (SELECT d FROM D d where d.propertyFromD='value')


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