【问题标题】:Hibernate @Formula building query incorrectlyHibernate @Formula 错误地构建查询
【发布时间】:2011-06-15 15:36:44
【问题描述】:

我正在向字段添加公式:

@Formula(value = "(select count(*) from approvalGroup as a where a.isAccounting=true)")

但是查询失败了,因为 Hibernate 试图在我的对象上设置“真”字段。
例外:

[ERROR] Unknown column 'approvalgr0_.true' in 'where clause'

我如何告诉 Hibernate 这是一个常量值而不是它需要从实体对象中检索的值?

【问题讨论】:

    标签: hibernate annotations formula


    【解决方案1】:

    Josh,Hibernate 公式被应用为原生 SQL(不是 HQL),并且您的 DBMS 的 SQL 方言可能没有 true 关键字。尝试如下更改代码

    @Formula(value = "(select count(*) from approvalGroup as a where a.isAccounting)")
    

    同时使用 DB 列名,而不是使用持久实体属性的名称。

    【讨论】:

    • 感谢您的回复。我刚刚发现 @Formula(value = "(select count(*) from approvalGroup as a where a.isAccounting=1)") 也可以。另外,我使用的是 sql 和 db 列名。
    • 但是你不觉得“=1”是多余的吗?
    • @Trader001 如果列类型也允许其他值,则不会。
    猜你喜欢
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2018-12-07
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    相关资源
    最近更新 更多