-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria building - trying to join another class on id
PostPosted: Thu Nov 08, 2007 12:29 pm 
Newbie

Joined: Fri Jul 16, 2004 3:32 pm
Posts: 3
I am using Hibernate3 (with the Spring Framework), and I am trying to build a criteria that would join a table/class just using the ID.

For example: let's say I have a class "Sale" that has an attribute "customerId". I also have a class "Customer" (and it has an attribute "city").

(Yes, the mapping file for Sale doesn't have a property for Customer, but customerId).

If I wanted to get the list of Sales that were made by Customers in New York, how would I be able to go about that using a Criteria?

Thanks in advance, Andy

PS. This is what I'm starting with:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Sale.class, "s");


Something like wouldn't be useful, right?:
Code:
DetachedCriteria customerCriteria = DetachedCriteria.forClass(Customer.class, "c");
customerCriteria.add( Restrictions.eqProperty("s.customerId", "c.id") );


Top
 Profile  
 
 Post subject: I think I've got a working solution
PostPosted: Fri Nov 09, 2007 11:54 am 
Newbie

Joined: Fri Jul 16, 2004 3:32 pm
Posts: 3
In case anyone is searching for a solution for this problem, I think I might got one. And if anyone sees some inconsistencies in it, please let me know.

Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Sale.class, "s");

DetachedCriteria customerSubquery = DetachedCriteria.forClass(Customer.class, "c")
   .setProjection( Property.forName("id") )
   .add( Restrictions.eqProperty("s.customerId", "c.id") )
   .add( Restrictions.eq("c.id", customerIdString) );

criteria.add( Subqueries.exists(customerSubquery) );

_________________
Thank you,
Andy Sears


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