Implementation of JPQL constructor_expression does not comply to JPQL Specification

Based on JPA specification, as the following BNF of JPQL shows, it is possible to have several constructor_expression-s along with other select_item-s in select_clause but is not the case in Hibernate.

select_clause ::= SELECT [ DISTINCT] select_item {, select_item}*
select_item ::= select_expression [[AS] result_variable]
select_expression ::=
        single_valued_path_expression |
        scalar_expression |
        aggregate_expression |
        identification_variable |
       OBJECT(identification_variable) |
       constructor_expression
constructor_expression ::=
       NEW constructor_name ( constructor_item {, constructor_item}* )

As it is clear from BNF, it is possible to use constructor_expression along with other select items but when when we have a query with several constructor_expression or constructor_expression along with other select_item-s, Hibernate complains about query syntax with the following exception :

ERROR [org.hibernate.hql.internal.ast.ErrorTracker] (default task-47) line 3:2: unexpected token: ,: line 3:2: unexpected token: ,
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1077)
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:748)
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:319)
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:198)
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:289)
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:188)
	at org.hibernate@5.3.20.Final//org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143)
	at org.hibernate@5.3.20.Final//org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119)
	at org.hibernate@5.3.20.Final//org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
	at org.hibernate@5.3.20.Final//org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
	at org.hibernate@5.3.20.Final//org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:611)
	at org.hibernate@5.3.20.Final//org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:720)
	at org.hibernate@5.3.20.Final//org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
	at org.jboss.as.jpa@22.0.1.Final//org.jboss.as.jpa.container.AbstractEntityManager.createQuery(AbstractEntityManager.java:448)
.........................................
.........................................

......  select 
                 new serp.base.xt.view.builder.model.ProcessVariable(ipv.variableName, ipvDt.code, ipv.businessKey)
                 , new serp.base.xt.view.builder.model.ProcessVariable(ipv.variableName, ipvDt.code, ipv.businessKey)
                 from DataElement de 
                 left join de.inputProcessVariable ipv
                 left join ipv.dataType ipvDt
                 left join de.outputProcessVariable opv
                 left join opv.dataType opvDt
....................................
....................................

platform info :

  • Hibernate Version : 5.3.20

  • wildfly application server : 22

  • JDK : Open JDK 16

I believe this is fixed in Hibernate 6.0 already, but not sure how hard it is to fix this in 5.x also. An easy workaround is to simply not use the constructor expression and instead build the objects manually from the result tuples.