-->
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 with 2 associations - how?
PostPosted: Tue May 18, 2004 7:11 am 
Newbie

Joined: Mon Jan 12, 2004 6:23 am
Posts: 5
I have a class which has two 1-many associations. My query should filter the result set on both associations. The Hibernate documentation just gives an example with a criteria on one associations:

Code:
List cats = sess.createCriteria(Cat.class)
.add( Expression.like("name", "F%")
.createCriteria("kittens")
.add( Expression.like("name", "F%")



How would I add an other criteria say to a 1-many association mates?

This does not work:

Code:
List cats = sess.createCriteria(Cat.class)
.add( Expression.like("name", "F%")
.createCriteria("kittens")
.add( Expression.like("name", "F%")
.createCriteria("mates")
.add( Expression.like("name", "T%")


Because Hibernate thinks that I want to add the criteria mates to the kittens.

Peter.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 7:21 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Have a look at http://www.hibernate.org/hib_docs/reference/en/html/querycriteria.html, section 12.4. Associations. There is a little bit there about using aliases.

Your code would look something like the following:

Code:
List cats = sess.createCriteria(Cat.class)
.createAlias("kittens", "kt")
.createAlias("mates", "mt")
.add(Expression.like("name", "F%")
.add( Expression.like("kt.name", "F%")
.add( Expression.like("mt.name", "T%");


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.