问题描述
我需要帮助一个棘手的hibernate查询问题。我有以下实体:
I need assistance with a tricky hibernate query problem. I have the following entities:
public class Book {
private String bookId;
private String author;
private String isbn;
private Set<Tag> tags;
// getters, setters etc.
}
和
public class Tag {
private String tagId;
private String tagName;
// getters, setters, etc.
}
两者之间的多对多关联,由连接表books_tags_mn与列book_id和tag_id表示。
There is a many-to-many association between the two that is represented by a join table books_tags_mn with the columns book_id and tag_id.
我喜欢做的是以下内容:创建一个hibernate查询/条件查询,返回具有某组标签全部的所有图书。
What I like to do is the following: I want to create a hibernate query/criteria query that returns all book that have all of a certain set of tags. What does work is to select all books that have any of a set of tags.
我一直在使用标准API,但是我已经搞定了一些标准API。没有真正理解它。所以我想要做的(在伪HQL)
I've been messing around with the criteria API but did not truly understand it. So what I am trying to do (in pseudo HQL)
from Book book where book.tags containsAll(:tags)
任何帮助将非常感谢,所以非常感谢你。
Any help on this would be highly appreciated, so thank you very much in advance.
推荐答案
您可以使用以下查询:
select book from Book book
where :numberOfTags = (select count(tag.id) from Book book2
inner join book2.tags tag
where book2.id = book.id
and tag in (:tags))
其中 numberOfTags
是必须匹配的标记集中的标记数。
where numberOfTags
is the number of tags in the set of tags that must be matched.
这篇关于Hibernate:选择实体,其中集合包含所有指定的valus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!