Matching *ALL* items in a list with Hibernate criteria

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;
}

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.

Thanks


你可以使用下面的查询,我会让你翻译成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'))
链接地址: http://www.djcxy.com/p/36986.html

上一篇: Hibernate对收集元素施加限制与获取模式冲突

下一篇: 使用Hibernate标准匹配列表中的* ALL *项