-->
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: createCriteria with realted classes and 'or' condition
PostPosted: Wed Sep 29, 2010 10:16 am 
Newbie

Joined: Wed Sep 29, 2010 9:39 am
Posts: 1
I've problems understanding how to create a criteria query with related classes and 'or' condition

some pseudo code...
Code:
class Customer {
  private String name;
}

class Order {
  private Customer deliverTo;
  private Customer billTo;
}


I want to create a query that finds all orders which are related to a customer (e.g. John) either by the deliverTo or the billTo relation.
I don't get how to to this with the criteria api.

I think the Expression.or could do the trick but how to use this with the related classes?


Top
 Profile  
 
 Post subject: Re: createCriteria with realted classes and 'or' condition
PostPosted: Wed Sep 29, 2010 6:29 pm 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
Have a look at Restrictions.disjunction() method. It will be smth. like

DetachedCriteria criteria = DetachedCriteria.forClass(Order.class);
Customer deliverTo = getDeliverToCustomer(); //get your deliver to customer
//same for billTo customer
Disjunction addressRestrictions = Restrictions.disjunction();
addressRestrictions.add(Property.forName("deliverTo").eq(deliverTo))
addressRestrictions.add(Property.forName("billTo").eq(billTo))
criteria.add(addressRestrictions);

See the generated SQL and make sure it is what you want.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.