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?