-->
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.  [ 4 posts ] 
Author Message
 Post subject: criteria api, collections and outer joins
PostPosted: Thu Jan 17, 2008 12:27 pm 
Newbie

Joined: Thu Jul 12, 2007 9:11 am
Posts: 3
I am using the criteria api to perform an outer join to limit the number of objects returned and its associated collections.

For example I want to return all Dog objects where its collection of puppy objects has a colour of black.

Using the criteria api and performing a left outer join this works fine. However when users come to use my Dog objects (in this case via JPA) when they call getPuppies() they are returned only the puppies where their colour is equal to black, when really I want all puppies in the collection to be returned? I have tried forcing a FetchMode of eager however this proves unsuccessful. Once i have limited the list of Dogs i dont care anymore wether its puppies are black or not?

How can I return all of the puppies collection in my Dog class when limiting by a puppy value of black?

Thanks

Andy,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 1:10 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
I am not very good with HQL expressions, there might be other ways, but:

Code:
select dog from Dog dog where exists(select dog.puppies pup where pup.color='black')


"exists" evaluates to "true" if at least one row is returned; therefore, this returns you all the Dogs, each with its complete litter, provided that at least one puppy in the corresponding litter is black.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 18, 2008 5:40 am 
Newbie

Joined: Thu Jul 12, 2007 9:11 am
Posts: 3
Thanks Gonzalo,

I will take a look at 'exists' as you suggest. My query is obviously not for Dog objects! but part of a rather complex criteria query. I wonder how I can do this using the criteria api? I will investigate.

Thanks,

Andy.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 25, 2008 6:10 pm 
Newbie

Joined: Wed Apr 18, 2007 8:58 pm
Posts: 1
Just looking through the (NHibernate) documentation and saw this in section 12.4, might point you in the right direction:

Quote:
Note that the kittens collections held by the Cat instances returned by the previous two queries are not pre-filtered by the criteria! If you wish to retrieve just the kittens that match the criteria, you must use SetResultTransformer(CriteriaUtil.AliasToEntityMap).


so, I'm guessing you shouldn't be using SetResultTransformer(CriteriaUtil.AliasToEntityMap).

pat


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