将列表中的* ALL *个项目与休眠条件匹配 [英] Matching *ALL* items in a list with Hibernate criteria
问题描述
所以我有一个Hibernate实体(简称为Zoos),其建立的一对多关系如下:
So I have a Hibernate entity(lets call it Zoos) with a many-to-many relationship set up like this:
@ManyToMany(cascade = {})
@JoinTable(name = "animal",
joinColumns = @JoinColumn(name = "zoo_id"),
inverseJoinColumns = @JoinColumn(name = "animal_id"))
@LazyCollection(LazyCollectionOption.FALSE)
public List<Animal> getAnimal() {
return animals;
}
所以现在我想找到所有带有狮子,老虎和熊。现在,我不在乎它们是否有其他动物,但我不希望动物园里只有老虎和mar猴。给定动物名称列表以匹配列表中的所有元素,我应该使用哪种标准?如果我使用Restricions。在这样的动物园里,我会拥有至少一个但不一定所有所要求的动物。
So now I want to find all zoos with the animals "lions", "tigers", and "bears". Now I don't care if they have other animals or not, but I don't want zoos with only tigers and marmosets. What kind of criteria should I use given a list of animal names to match all elements of the list? If I use Restricions.in I will get zoos that have at least one, but not necessarily all of the animals requested.
谢谢
推荐答案
您可以使用以下查询,让我将其转换为Criteria:
You could use the following query, that I'll let you translate to Criteria:
select zoo from Zoo zoo
where 3 = (select count(distinct animal.name) from Zoo zoo2
join zoo2.animals animal
where zoo2.id = zoo.id
and animal.name in ('lion', 'tiger', 'bear'))
这篇关于将列表中的* ALL *个项目与休眠条件匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!