2013-08-28 36 views
3

我有一个实体Entry它有以下关系:如何以选择所有的列表元素

@Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @ManyToMany(mappedBy = "entryList", cascade = {CascadeType.ALL}) 
    private List<Tag> tags = new LinkedList<>(); 

此SELECT语句选择所有Entry其中的关系tags是从列表中至少一个元素list

SELECT m FROM Entry m JOIN m.tags tags WHERE tags IN :list; 

但我要的是一个SELECT语句选择所有Entry其中全部list的元素必须与tags有关?

回答

1

我认为你需要使用子查询和计数,

Select e from Entry e where (Select count(t) from Tag t, Entry e2 join e2.tags t2 where t2 = t and e = e2 and t.id in (:ids)) = :size 

凡ID是标签的ID(您可以使用对象以及),以及:大小大小的标签集合。

如果标签具有逆的M-M进入就可以使用,

Select e from Entry e where (Select count(t) from Tag t join t.entries e2 where e = e2 and t.id in (:ids)) = :size 
+0

谢谢。好主意使用子查询和计数! – timmornYE

相关问题