I use the following query to get a java.util.Map
with indexes id
, text
and object
:
Query q = mySession.createQuery(
"SELECT u.id AS id, u.name AS text, u AS object FROM User u")
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
... but object
seems to be a reserved word. For example obj
is OK. What is the current way to escape an alias in HQL the way MySQL uses backtick escapes?
Using backtick gives the following error:
Exception in thread "main" org.hibernate.QueryException: unexpected char:
'`' [SELECT u.id AS id, u.name AS text, u AS `object` FROM User u]
You could achieve it by a workaround using your custom "alias to map" transformer, so your code would change to something like this
And then using this class:
On a related topic, escaping a reserved word in a column name is as easy as prepending with a table alias. No backticks, square brackets, etc. Simply replace
by:
(note: i'm NOT saying it's a good idea to have "where" as column name ;-)
Do backticks not work on aliases? The Hibernate documentation says to use them for fields.
Other questions:
the back tics concept works in hibernate.. unfortunately this only works if you've done annotation config...
you can try to do this in a different way (without annotations).
here you need to create a constructor in use which accepts an id, name, object elements & in that order.