-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria nested search
PostPosted: Tue Jan 13, 2004 10:58 pm 
Newbie

Joined: Tue Jan 13, 2004 10:37 pm
Posts: 2
First off, congrats on the amazing effort by the Hibernate team. It's simplified our application in so many ways.

I'm trying to use the Criteria API to search nested properties of a class. This is a topic that's been covered in this forum before, but I haven't found this PARTICULAR question.

The simplest way to describe the problem would be to use the Cat/Kitten analogy from Chapter 14 of the hibernate docs....

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


This gives back all the cats starting with 'F' who have kittens that start with 'F'.

The question is, what code would you add to return all the cats starting with 'F' who have kittens starting with 'F' AND kittens starting with 'G'?

My application has nothing to do with cats, but the relationships are pretty much same.

I've tried creating a conjunction/disjunction, on the "kittens" criteria, but the resulting sql basically tried to find a single kitten named 'F%' AND 'G%', which is obviously impossible.

I've tried creating two criteria objects (both kittens), but the resulting sql is incorrect. The joins get messed up because of the like-named criteria, I guess. Here's the code for that case...

Code:
Criteria cats = session.createCriteria( Cat.class);
cats.add( Expression.like("name", "F%"));

Criteria k1 = cats.createCriteria("kittens");
k1.add( Expression.like("name", "F%"));

Criteria k2 = cats.createCriteria("kittens");
k2.add( Expression.like("name", "G%"));


Thanks for any advice....I feel like I've exhausted my options.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 2:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You would need to use a custom Criterion or else a subquery in HQL


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 10:21 pm 
Newbie

Joined: Tue Jan 13, 2004 10:37 pm
Posts: 2
Gavin or anyone else kind enough to be reading,

I explored the idea of modifying CriteriaImpl to support multiple calls to createCriteria() with the same associationPath, but quickly realized it would end up touching a lot of code.

I'm very willing to explore creating new Criterion, but am unsure how to proceed with that.

This is the HQL (returns cats that have a kitten with a red toy and a kitten with a blue toy)

Code:
from Cat as c
left join fetch c.kittens as k1
left join fetch c.kittens as k2
where k1.toy.colour= 'Red' and
k2.toy.colour='Blue'


How would I go about creating new criterion that could handle this? I've read over an example that was posted here for a 'NOT IN' expression, but since this creates additional joins, I wasn't sure which methods to call to get the job done.

To be clear, cat to kitten is a many to many, kitten to toy is many to one.

Thanks for any advice.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2004 2:52 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Ok, I might be missing something but what you might actually be trying to do is not find kittens whose names begin with F% AND G% but kittens whose names begin with F% OR G%. In that case, just use the Expression.or method and build it up using your 2 search conditions.

You already said that it created a SQL expression that is impossible to return any results. However, it IS returning exactly what you were asking for. I think the problem is simply a flaw in your logic. If I'm off base then just ignore me ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2004 3:01 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Ok, just reread for about the 5th time :)

First of all, where's the edit button? lol.

Secondly, I may have misinterpreted what you were trying to acomplish and upon re-reading see that you may have intended to find cats that have at least one kitten whose name begins with F% and at least one that begins with G%.

If that is the case then you can truly ignore my previous post.


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