-->
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.  [ 3 posts ] 
Author Message
 Post subject: problem creating simple HQL query with a collection
PostPosted: Tue Mar 02, 2010 6:58 am 
Newbie

Joined: Tue Mar 02, 2010 6:40 am
Posts: 3
I have two objects with a many-to-many relationship, to stay with the examples in the reference manual say I have a Cat object which has one or more kitten objects in a Set 'kittens'. I run into trouble when I want to make a simple query to fetch all Cats who have a kitten of a specific color.

I tried something like:

Code:
from Cat as cat WHERE cat.kittens.color = 'green'


this gives "illegal attempt to dereference collection" exception however. How can I tell Hibernate to simply go through the colors of the kittens of a Cat and compare them to a given parameter?

Thanks for any help!


Top
 Profile  
 
 Post subject: Re: problem creating simple HQL query with a collection
PostPosted: Tue Mar 02, 2010 7:52 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Code:
select cat from Cat as cat inner join cat.kittens as k WHERE k.color = 'green'


Hope can help you.


Top
 Profile  
 
 Post subject: Re: problem creating simple HQL query with a collection
PostPosted: Fri Mar 05, 2010 5:36 am 
Newbie

Joined: Tue Mar 02, 2010 6:40 am
Posts: 3
Thanks! I decided to go with Criteria instead of HQL though. So my "query" became something like this:

Code:
Criteria criteria = session.createCriteria(Cat.class);
criteria.createCriteria("kittens").add( Restrictions.eq("color", "green") );


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