[英]How to escape the keyword “on” in HQL
Here is my Entity
which I want to query with Hibernate
4.3.4: 这是我要使用
Hibernate
4.3.4查询的Entity
:
@Entity
@DynamicInsert
@DynamicUpdate
public class Device extends OrmEntity implements Serializable {
@Column(name = "is_on") // was necessary to get Hibernate to create the table at all
private boolean on;
...
}
This is the HQL I want to execute (btw the database is PostgreSQL
): 这是我要执行的HQL(但数据库是
PostgreSQL
):
Query query = session.createQuery("from Device where ... and on = :a");
query.setBoolean("a", true);
This gives me org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: on near line 1
. 这给了我
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: on near line 1
。
I tried many escape characters like backticks, \\", [ ], single-quotes, without success. 我尝试了许多转义字符,例如反引号,\\“,[],单引号,但均未成功。
What would be the appropriate way to escape the keyword "on" in this HQL-query? 在此HQL查询中转义关键字“ on”的合适方法是什么?
Edit: 编辑:
I tried "from Device d where d.on = :a"
which results in QuerySyntaxException: unexpected token: d near line 1
. 我尝试了
"from Device d where d.on = :a"
,这导致QuerySyntaxException: unexpected token: d near line 1
。
I did not try renameAlias
as this seems to be overkill for just escaping a keyword... 我没有尝试过
renameAlias
因为这似乎只是为了逃避一个关键字renameAlias
...
Edit2: EDIT2:
I am trying out AliasToMapTransformer
but I can not seem to get it working for a where
clause ... ? 我正在尝试
AliasToMapTransformer
但似乎无法使它在where
子句中起作用...? It seems to be made for column aliases. 它似乎是为列别名创建的。
Edit3: EDIT3:
Generally, this code is around for some time already and strangely it worked with Hibernate
< 4.3.$ 通常,这段代码已经存在了一段时间,奇怪的是它与
Hibernate
<4.3。$一起工作。
Edit4: Edit4:
I accepted NimChimpsky's answer, and implemented the following change: Rename the boolean member from on
to isOn
, but keep the getter and setter as they were: setOn()
and isOn()
. 我接受了NimChimpsky的回答,并进行了以下更改:将布尔成员从
on
重命名为isOn
,但将getter和setter保持不变: setOn()
和isOn()
。
No further change in my code necessary, and Hibernate
is happy now. 无需对我的代码做进一步更改,
Hibernate
现在感到高兴。
@Column
private boolean deviceIsOn;
annotate the field with a renamed variable, and then my method would be isOn
. 用重命名的变量注释字段,然后我的方法是
isOn
。
Bit hacky ... but you keep your nice friendly OO api, with out too much hassle. 有点hacky ...但是您保留了友好的OO api,没有太多麻烦。
Or even simpler, just call it off
which isn't reserved 甚至更简单,只需将其
off
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.