Nested "treat (x as Type)" aren’t supported

Description

Section 4.4.4.1 of the JPA 2.1 specification (which gives the syntax for path expressions) explicitly allows nested "treat as" statements, yet they are not currently supported by Hibernate. The following test case throws a QuerySyntaxExpression:

TreatTest.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class TreatTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { return new String[] { "hql/Animal.hbm.xml" }; } @Test public void nestedTreatTest() { final Session s = openSession(); s.createQuery( "from Animal h where treat ( treat ( h as Human ).father as Human ).pets is empty" ); } }

The stacktrace of the thrown expression is:

1 2 3 4 5 6 7 8 9 10 11 12 13 org.hibernate.hql.internal.ast.QuerySyntaxException: expecting "as", found '(' near line 1, column 58 [from org.hibernate.test.hql.Animal h where treat ( treat ( h as Human ).father as Human ).pets is empty] at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91) at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:239) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800) at org.hibernate.test.hql.TreatTest.whereTreatTest(TreatTest.java:95)

My pull request for added support for nested "treat as" just after a "member of" construction, but it is currently the only place where nested treat as are supported. This new bug report is in order to support them everywhere where they should be allowed.

Environment

None

Status

Assignee

Unassigned

Reporter

Etienne Miret

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.3.6
5.0.1

Priority

Major
Configure