-->
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.  [ 12 posts ] 
Author Message
 Post subject: hql query that returns parent only if none of child objects
PostPosted: Fri Sep 30, 2005 6:13 pm 
Beginner
Beginner

Joined: Tue Apr 12, 2005 9:15 pm
Posts: 24
Hello,
I need help in finding a way how I can create an hql query that would return me parent object only if none of the child object match a where clause.

e.g I have a parent->child relation between Person->Pets. Pets is a Set on Person. I want the hql query to be - return the person objects from db where the person object doesn't have any Pets that has the petName property value asDog.
Select * from Person left outer join Person.Pets pet where ???

I am not sure what the where could be here in hql to have pet.petName != Dog for any of the objects in my Set Pets.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 6:18 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
from Person p where p.pets.size = 0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 7:55 pm 
Beginner
Beginner

Joined: Tue Apr 12, 2005 9:15 pm
Posts: 24
actually I can't do from Person p where p.pets.size = 0 because I need to check for the Set pets of person where none of the pet has the property petName = Dog - the set pets may contain objects, but none of them would be the one with petName = Dog.

Unfortunately, my collection here is a subclass of a baseclass and the property petName is on my baseclass - so, I can't apply hibernate filter here. And, I need to do this as part of query itself and return only the Person objects that match my criteria. Anyone has come across such query?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 8:04 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
ok, not sure why I went w/ size ...

try "select p.person from Pet p where p.petName != 'foo'"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 8:14 pm 
Beginner
Beginner

Joined: Tue Apr 12, 2005 9:15 pm
Posts: 24
this doesn't work for me because if I do
select * from person p left outer join p.pets pet where pet.petName != 'dog', if I have the person "XXX" with set pets containing e.g 'dog' and 'cat', because one of the set object is not a dog (in this case, it is true because the set had 2 objects - and one of them has petName= 'cat' is not a dog), this select query returns me the person object "XXX" even though it does contain a pet with name 'dog'.

so, I am really not sure how I do such query with HQL. Any other suggestions? please?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 10:40 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
look, I misinterprettted you at first but I simply did not suggest a join. i'll help you on this but I don't really know what you are asking? do you realize that last post is all one sentence?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 12:04 am 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
select p
from Person p
left join p.pets q
where q.name != ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 8:58 am 
Beginner
Beginner

Joined: Tue Apr 12, 2005 9:15 pm
Posts: 24
Dennis,
Yes sorry for not explaining it right. In your second answer - you have select p.person from Pet p where p.petName != 'foo'. But that is not what I want. I want to do select query for a person and not pet. And the where clause I want is something that would get me the person ONLY if none of the pets for that person are with pet.petName = 'dog'.....

so, something like this for a select query(I am not writing the exact sql since I don't know what would go in where) :
select * from Person p where NOT (p.pets.petName = 'dog');

meaning return me the object pet ONLY if none of the pet I have in set pets is with petName = dog.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 1:55 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Did you read my post?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 2:00 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
Actually, that should probably be an INNER JOIN, not a LEFT JOIN.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 5:49 am 
Beginner
Beginner

Joined: Mon Sep 22, 2003 5:18 am
Posts: 28
Does you database support subselects? If it does, than you should try to play with 'exists' query keyword.
This could be like this:


select * from Person p
where not exists(select pet from p.pets where p.petName = 'dog');


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 5:56 am 
Beginner
Beginner

Joined: Mon Sep 22, 2003 5:18 am
Posts: 28
i.e. to be more correct in terms of HQL syntax, last should be read as follows:

Code:
select * from Person p
where not exists(select from p.pets pet where pet.petName = :petName);


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