Hibernate - where子句任何带有集合作为命名参数查询

时间:2016-11-29 11:57:18

标签: java hibernate jpa

我想知道是否可以使用像查询一样的集合进行此类评估,而不是在我的项目中使用名为查询的类:

@NamedQueries(value = {  
@NamedQuery(
name = "Foo.getBarList", 
query = "select f from Foo f where (:barlist is null or f.barlist.id in (:barslist)) " )})

从这两个帖子:

FROM Foo WHERE Id = :id AND (:barlist is null or Bar in (:barlist)) JPA where clause any

Hibernate HQL Query : How to set a Collection as a named parameter of a Query?

我猜问题是查询中的id,所以我删除并且错误停止,只需更改此信息:
  "select f from Foo f where (:barlist is null or f.barlist in (:barslist)) "

1 个答案:

答案 0 :(得分:1)

而不是在HQL中,您可以检查

if(barlist==null){
    //query=FROM Foo WHERE Id = :id
}else{
   //query=FROM Foo WHERE Id = :id AND Bar in :barlist
   // query.setParameterList(:barlist",<list object>)
}