2011-06-15 96 views
4

我加入一个公式字段:休眠@Formula建筑查询错误

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

但查询失败,因为Hibernate试图让我的对象“真实”的字段。
例外:

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

我怎么能告诉Hibernate,这是一个恒定值,这也不是它需要从实体对象中检索?

回答

10

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

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

还可以使用DB列名,而不是使用持久实体属性的名称。

+0

感谢您的回复。我刚刚发现@Formula(value =“(从approvalGroup中选择count(*)as a.isAccounting = 1)”)也适用。另外,我正在使用sql和db列名。 – Josh 2011-06-15 16:09:46

+1

但是你不觉得“= 1”是多余的吗? – Trader001 2011-07-05 15:09:48

+0

@ Trader001如果列类型也允许其他值,则不适用。 – 2014-01-15 22:49:40