JPQL where子句与集合中的集合 [英] JPQL where clause with collection in collection
问题描述
我使用spring数据...我有一个ManyToMany关系实体。
所以我想要做这样的请求:
@Query(SELECT d FROM Data d WHERE((?2)IS NULL or d.categories IN(?2)))
其中 但是这不起作用......任何想法? :) PS:我看到一个解决方案: 但是这是一个由hibernate产生的蹩脚请求,并不完全是我想要的... 查看这是否可行 如果?2为null,则上面的查询将从Data表中检索所有记录,其他将根据您将传递的类别ID列表进行过滤?2。 I use spring data... and I have an entity with ManyToMany relation. So I wanna do this kind of request: where But this doesn't work... any idea ? :) PS: I see a solution with: But it's a crappy request generated by hibernate and isn't exactly what I want... See if this works
The above query will retrieve all the records from Data table if ?2 is null other it will filter based on the list of category ids you will pass as ?2. 这篇关于JPQL where子句与集合中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!?2
对应一个清单, categories
也是
@查询( 设定R FROM数据R WHERE(λ3= 0L ORθ3 =(SELECT COUNT(cate.id)FROM数据R2 JOIN r2.categories美食WHERE r.id = r2.id AND美食IN?2)) )
@Query(SELECT DISTINCT(d)FROM Data d JOIN d.categories c
WHERE(COALESCE(?2,NULL)is NULL or c.id IN(?2)))
@Query("SELECT d FROM Data d WHERE ((?2) IS NULL OR d.categories IN (?2))")
?2
corresponds to a list and categories
is also a list (of another entity) from Data
.@Query("SELECT r FROM Data r WHERE (?3 = 0L OR ?3 = (SELECT COUNT(cate.id) FROM Data r2 JOIN r2.categories cate WHERE r.id = r2.id AND cate IN ?2 ))")
@Query("SELECT DISTINCT(d) FROM Data d JOIN d.categories c
WHERE (COALESCE(?2, NULL) IS NULL OR c.id IN (?2))")