-->
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: Criteria
PostPosted: Fri Jan 23, 2004 4:42 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
How can I use the Criteria class to create a query like this.

Code:
select * from cats, kittens left join kittens.parent_id on cat.id where (cat.name="Pussy" or kitten.name="Kitten")


I have not tested the query above but you should/could maybe understand what I'm trying to do.


In Hibernate I can do the following.


Code:
criteria.createCriteria("cat").add(Expression.eq("name", "Pussy"));
criteria.createCriteria("kittens").add(Expression.eq("name", "Kitten"));



But I cannot figure out how to get the OR into the query. Since Expression.conjunction does not take a Criteria as an argument.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 5:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You might try something like this:

List cats = sess.createCriteria(Cat.class)
.createAlias("kittens", "kt")
.add( Expression.conjunction(Expression.eqProperty("kt.name", "something"), Expression.eqProperty("name", "somethingelse" )
.list();


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Sat Jan 24, 2004 3:56 am 
Beginner
Beginner

Joined: Fri Jan 23, 2004 4:29 pm
Posts: 39
Thanks for pointing me in the right direction. One correction, conjunction does not take any arguments.

Code:
List cats = sess.createCriteria(Cat.class)
.createAlias("kittens", "kt")
.add( Expression.conjunction().add(Expression.eq("kt.name", "something")).add(Expression.eq("name", "somethingelse" ))
.list();


It would be good to have conjunction and discjunction examples in the Hibernate documentation.


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.