Using Reserved Keyword in Table Names throws org.hibernate.hql.internal.ast.QuerySyntaxException in Hibernate 5.2.11

Hello Team,

I have upgraded the Hibernate version from Hibernate ORM 4.3 to 5.2.11 version, we are using the hibernate framework using .hbmxml files.

I came across a use-case in which the table Name is a reserved Keyword i.e where , I checked for the details about how to escape the same using identifiers by using the following settings in hibernate.cfg.xml

<property name="hibernate.globally_quoted_identifiers">true</property>
<property name="hibernate.globally_quoted_identifiers_skip_column_definitions">true</property>

However when i try to execute it in SQLServer, i am getting the following exception

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: Where near line 1, column 6 [from Where]

The detailed stack trace is as follows

Hibernate:
    select
        max(ID)
    from
        [
    Where
        ]
Hibernate:
    insert
    into
        [

    Where
        ] (
            PropNumber, PropName, ID
        )
    values
        (?, ?, ?)
09/27 12:07:48 [http-nio-8500-exec-2] ERROR line 1:6: unexpected token: Where
09/27 12:07:48 [http-nio-8500-exec-2] ERROR line 1:6: unexpected token: Where
line 1:6: unexpected token: Where
        at org.hibernate.hql.internal.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1516)
        at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1340)
        at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1060)
        at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:745)
        at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:316)
        at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:198)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:283)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:186)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
        at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
        at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
        at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:546)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:655)
        at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:102)

I wanted to know if i am missing on some settings or if this is an issue while parsing? Please let me know if you need more details, i would be happy to share the same.

Appreciate your help in Advance.

Thanks,
Ashudeep

Just because you quote your SQL identifiers doesn’t mean that you can use these keywords as identifiers in HQL. I would suggest you to use the fully qualified name or rename the entity class, or set an entity name via @Entity(name = "Where_").

@beikov Do you mean to say we can’t use these keywords even after enabling quoted Identifiers, we are using the hbmxml format for hibernate Query Generation and this is how the xml looks like

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class entity-name="Where" lazy="true" name="xyz:Where" table="`Where`">
    <id name="ID" type="int">
      <column name="ID"/>
      <generator class="increment"/>
    </id>
    <property name="PropNumber" type="string">
      <column name="PropNumber"/>
    </property>
    <property name="PropName" type="string">
      <column name="PropName"/>
    </property>
  </class>
</hibernate-mapping>

What changes do i need to do in this so that it works fine, As per my analysis this breaks when Hibernate Parser parses the SQL Queries generated for this entity.

The Queries that are generated are

Hibernate:
select
max(ID)
from
[
Where
]
Hibernate:
insert
into
[

Where
    ] (
        PropNumber, PropName, ID
    )
values
    (?, ?, ?)

Do let me know your thoughts on this what needs to be changed for this to work?

Thanks,
Ashudeep

The error is about HQL parsing, not SQL parsing, so the problem is in your HQL query. You need to change the entity name to a non-keyword.

Thanks @beikov for the explanation. I can understand now.