I have an an object (BlogPost) that contains an M:N collection of elements (Tags).
How to query for an object (BlogPost) where at least one it its Tags matches an element in a set of Tags (defined by the user) with JPA2 (Hibernate).
findBlogPostWithAtLeastOneMatchingTag(Collection<Tag> tags){ ???? }
My main problem is, that I actually need to compare two collections of tags:
- the collection of tags of the BlogPost.
- the collection I search for
I tried Select p from Post p where p.tags in(:tags)
but it does not work, as my post entities have more than just one tag.
So what could I do instead?
My BlogPost entity looks like this. It has several Tags.
@Entity
public class BlogPost{
/** The tags. */
@ManyToMany()
@NotNull
private Set<Tag> tags;
@NotBlank
private String content;
...
}
The solution must not be JPQL, JPA-Criteria (not Hibernate-Criteria) would be fine too.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…