Search code examples
hibernatehqlaliasjpqlcase-sensitive

Are HQL table aliases case sensitive?


The scenario is the following. There was a hql query in our application like

select a.status from table a where A.status = 1

This was working in hibernate3. But when upgraded to hibernate 5.0.12 and jpa 2.1, this query is not working. Gives the following exception:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path:

When we updated the alias as in same case, this is working.

select a.status from table a where a.status = 1

But the official documentation states that the alias name can be case insensitive.

So this is a bug or HQL alias names are case sensitive from hibernate 5 onwards.


Solution

  • Actually, JPQL states that aliases must be treated as case-insensitive:

    15.10. Identification variables

    Identification variables are often referred to as aliases. References to object model classes in the FROM clause can be associated with an identification variable that can then be used to refer to that type throughout the rest of the query.

    In most cases declaring an identification variable is optional, though it is usually good practice to declare them.

    An identification variable must follow the rules for Java identifier validity.

    According to JPQL, identification variables must be treated as case-insensitive. Good practice says you should use the same case throughout a query to refer to a given identification variable. In other words, JPQL says they can be case-insensitive and so Hibernate must be able to treat them as such, but this does not make it good practice.

    And it was the hibernate issue HHH-8546 that looks like resolved, but not in the version you are interested in.

    In any way, in order to avoid headache, and to make code cleaner, it is good to follow good practice.