HQL table 别名区分大小写吗?

Are HQL table aliases case sensitive?

场景如下。我们的应用程序中有一个 hql 查询,如

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

这是在 hibernate3 中工作的。但是当升级到 hibernate 5.0.12 和 jpa 2.1 时,这个查询不起作用。给出以下异常:

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

当我们像在相同的情况下更新别名时,这是有效的。

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

但是官方文档states说别名可以不区分大小写

所以这是一个错误,或者 HQL 别名从 hibernate 5 开始区分大小写。

实际上,JPQL 声明别名必须被视为 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.

休眠问题 HHH-8546 看起来已解决,但不是您感兴趣的版本。

无论如何,为了避免头痛,并使代码更清晰,遵循良好的做法是很好的。