Question

Here is my Entity which I want to query with Hibernate 4.3.4:

@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):

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.

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?

Edit:

I tried "from Device d where d.on = :a" which results in QuerySyntaxException: unexpected token: d near line 1.

I did not try renameAlias as this seems to be overkill for just escaping a keyword...

Edit2:

I am trying out AliasToMapTransformer but I can not seem to get it working for a where clause ... ? It seems to be made for column aliases.

Edit3:

Generally, this code is around for some time already and strangely it worked with Hibernate < 4.3.$

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().

No further change in my code necessary, and Hibernate is happy now.

Was it helpful?

Solution

@Column
private boolean deviceIsOn;

annotate the field with a renamed variable, and then my method would be isOn.

Bit hacky ... but you keep your nice friendly OO api, with out too much hassle.

Or even simpler, just call it off which isn't reserved

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top