使用play框架在hibernate中绑定多个值

时间:2010-09-28 11:09:11

标签: java hibernate playframework

我正在尝试在Play框架中构建查询,我有

select * from Candidate c where (:schools member of c.schools) 

我绑定后:带有一个元素的List学校会返回结果,但如果我用多个元素绑定List,则没有任何反应。

Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: {vector} [select c from models.Candidate c where (:schools0_, :schools1_ member of c.schools)  group by c.id order by RAND()]

其实我需要像

这样的东西
select * from candidate where schools in (x,x,x,x,x);

候选人和学校之间的关系在链接表中。

有没有办法绑定多个值?

2 个答案:

答案 0 :(得分:4)

实际上我发现了问题所在 - 成员只能使用单个值,并且工作正常。当我们需要使用多个值时,最好使用中的标准sql

 select c from Candidate c inner join c.schools as school where school.id in (25980,25981)"

需要加入链接表 - 我们不能使用c.schools.id,因此我们需要使用别名内部连接c.schools来指定列。

我认为所有初学者都应该检查http://www.javatx.cn/hibernate/reference/en/html/queryhql.html

答案 1 :(得分:1)

使用Hibernate,您也可以直接使用列表本身。

select c from Candidate c join c.schools as school where school.id in (:schools)

根据您的ID键入:schools参数的类型,例如List<Int>List<Long>