Regression for "treat" function generate "Bad generatedAlias" for a query

Description

Just upgraded from Hibernate 5.1.0 Final to Hibernate 5.2.2 Final and got following produced JPQL from hibernate.

{{Invalid path: 'generatedAlias2.imgLocation' }}

The following code can reproduce the issue

Join<A, B> somefield= root.join(A.image, JoinType.INNER);
Predicate notEmpty =
cb.notEqual(cb.treat(somefield,B.class).get(B_.somefield), "");

We are using JPA criteria and mainly we do not write free text. It seems that the cause for the error is using treat function:
removing the use of treat function makes the issue disappear

SELECT count(generatedAlias0)
FROM com.centure.core.matching.entity.ImageForMatch AS generatedAlias0
INNER JOIN generatedAlias0.image AS generatedAlias1
WHERE (generatedAlias2.imgLocation<>aram0)
AND (generatedAlias3.photographerName IS NOT NULL)
AND (generatedAlias3.category=aram1)
AND (generatedAlias0.level=1)

It is clear that something went wrong and there generatedAlias2 and generatedAlias3 does not exist....

Sep 08, 2016 12:49:08 PM org.apache.catalina.core.StandardWrapperValve invoke

SEVERE: Servlet.service() for servlet [spring] in context with path / threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias2.imgLocation' [select count(generatedAlias0) from com.centure.core.matching.entity.ImageForMatch as generatedAlias0 inner join generatedAlias0.image as generatedAlias1 where ( generatedAlias2.imgLocation<>aram0 ) and ( generatedAlias3.photographerName is not null ) and ( generatedAlias3.category=aram1 ) and ( generatedAlias0.level=1 )]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias2.imgLocation' [select count(generatedAlias0) from com.centure.core.matching.entity.ImageForMatch as generatedAlias0 inner join generatedAlias0.image as generatedAlias1 where ( generatedAlias2.imgLocation<>aram0 ) and ( generatedAlias3.photographerName is not null ) and ( generatedAlias3.category=aram1 ) and ( generatedAlias0.level=1 )]] with root cause
org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'generatedAlias2.imgLocation' [select count(generatedAlias0) from com.centure.core.matching.entity.ImageForMatch as generatedAlias0 inner join generatedAlias0.image as generatedAlias1 where ( generatedAlias2.imgLocation<>aram0 ) and ( generatedAlias3.photographerName is not null ) and ( generatedAlias3.category=aram1 ) and ( generatedAlias0.level=1 )]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:268)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
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:152)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:526)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:628)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3285)
at org.hibernate.query.criteria.internal.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:318)
at org.hibernate.query.criteria.internal.compile.CriteriaCompiler.compile(CriteriaCompiler.java:127)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:3578)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:203)
at sun.reflect.GeneratedMethodAccessor763.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy129.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getCountQuery(SimpleJpaRepository.java:685)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getCountQuery(SimpleJpaRepository.java:659)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.count(SimpleJpaRepository.java:495)
at sun.reflect.GeneratedMethodAccessor1041.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:488)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy146.count(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1616.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.zeroturnaround.javarebel.integration.util.ReloadingProxyFactory$ReloadingMethodHandler.invoke(SourceFile:74)
at com.sun.proxy.$Proxy146.count(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1616.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.zeroturnaround.javarebel.integration.util.ReloadingProxyFactory$ReloadingMethodHandler.invoke(SourceFile:74)
at com.sun.proxy.$Proxy146.count(Unknown Source)
at com.centure.core.m

Environment

Windows,
Tomcat
<spring.core.version>4.3.2.RELEASE</spring.core.version>
<spring.data.jpa.version>1.10.2.RELEASE</spring.data.jpa.version>
<hibernate.entitymanager.version>5.2.2.Final</hibernate.entitymanager.version>

Assignee

Gail Badner

Reporter

koa

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Critical
Configure