【问题标题】:binding multiple values in hibernate using play framework使用播放框架在休眠中绑定多个值
【发布时间】:2010-09-28 11:09:11
【问题描述】:

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

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

在我将 :school 与 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);

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

有没有办法绑定多个值?

【问题讨论】:

    标签: java hibernate playframework


    【解决方案1】:

    实际上我发现问题出在哪里 - 成员 只能与单个值一起使用,并且工作正常。当我们需要使用多个值时,最好使用标准sql in

     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

    【讨论】:

      【解决方案2】:

      借助 Hibernate,您还可以直接使用列表本身。

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

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

      【讨论】:

        猜你喜欢
        • 2016-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-18
        相关资源
        最近更新 更多