-->
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: Hibernate criteria on Set<String> containsAll
PostPosted: Thu Mar 20, 2014 12:19 pm 
Newbie

Joined: Thu Mar 20, 2014 12:12 pm
Posts: 2
Location: Kyiv, Ukraine
Here is my problem: I need to retrieve images, which have all required tags

I have an Image class:

Code:
    Image {
       String name;
       Set<String> tags;
   }

It was fairly easy to search for images that contain any of the required tags:
Code:
    crit.createAlias("tags", "t", JoinType.LEFT_OUTER_JOIN);
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    Disjunction disjunction = Restrictions.disjunction();
    for (String tag : tags) {
        disjunction.add(Restrictions.eq("t.elements", tag));
    }
    crit.add(disjunction);

Now, I would like to retrieve Images that have all required tags. Let's say we have images:

Image-1, {tag1,tag2}
Image-2, {tag1,tag2,tag3}
Image-3, {tag2,tag3,tag4}
If I search for images with tags: {tag1,tag2}, I would like to get Image-1 and Image-2. With my current implementation, obviously, I get all 3 images.

I was trying to achieve this by using Projection, but couldn't do so. I am new to Hibernate and SQL. I believe, this is a trivial task, but I really need some help.

Any suggestions?


Top
 Profile  
 
 Post subject: Re: Hibernate criteria on Set<String> containsAll
PostPosted: Thu Mar 20, 2014 2:53 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Don't you just want to change your Disjunction (OR) to a Conjunction (AND)?

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: Re: Hibernate criteria on Set<String> containsAll
PostPosted: Thu Mar 20, 2014 5:27 pm 
Newbie

Joined: Thu Mar 20, 2014 12:12 pm
Posts: 2
Location: Kyiv, Ukraine
No, it's not working. If I change Disjunction to Conjunction generated SQL would look like:
Code:
image.tag = tag1 AND image.tag = tag2

which will return false on any Image.tag, unless tag1 == tag2


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.