Can not use Fetch as parameter of treat() for downcasting

Description

Dear hibernate community,

(I posted the question to https://discourse.hibernate.org/t/can-fetch-be-used-as-parameter-of-treat-for-downcasting/3301 but no response in several days. So I re-post here.)

I am trying to use Fetch instead of Join as parameter of treat(), to avoid the problem that the duplicate join is generated by hibernate (as mentioned in How can I do a JOIN FETCH in criteria api). I am using hibernate-core “5.4.5.Final” and Spring Boot “2.1.8.RELEASE”.

Say a fetch is: “Fetch<Object, Object> fetch = root.fetch(“friends”, JoinType.LEFT);”.

If call “criteriaBuilder.treat((SetJoin<?, ?>) fetch, (Class) SpecialDroid.class)”, below exception will be thrown:

java.lang.UnsupportedOperationException: null
at java.util.AbstractCollection.add(AbstractCollection.java:262) ~[na:1.8.0_211]
at org.hibernate.query.criteria.internal.CriteriaBuilderImpl.treat(CriteriaBuilderImpl.java:1400) ~[hibernate-core-5.4.5.Final.jar:5.4.5.Final]
at org.hibernate.query.criteria.internal.CriteriaBuilderImpl.treat(CriteriaBuilderImpl.java:1125) ~[hibernate-core-5.4.5.Final.jar:5.4.5.Final]
at com.open_care.template_app.ignite_h2.controller.JPAQueryController.getFieldPredicates(JPAQueryController.java:57) ~[main/:na]
at com.open_care.template_app.ignite_h2.controller.JPAQueryController.getAllEmployeesFromRepository(JPAQueryController.java:32) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at

If call “criteriaBuilder.treat((Path) fetch, (Class) SpecialDroid.class)”, later entityManager.createQuery() will report error:

org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: ‘generatedAlias1.secondaryFunction’ [select distinct human from com.open_care.template_app.ignite_h2.model.Human as human left join fetch human.friends as generatedAlias0 where treat(generatedAlias1 as com.open_care.template_app.ignite_h2.model.SpecialDroid).secondaryFunction like :param0]

Sounds that if hibernate can replace the ‘generatedAlias1’ in above query string with ‘generatedAlias0’, the problem can be solved.

Environment

None

Status

Assignee

Unassigned

Reporter

Baogang Liu

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.4.6
5.4.5

Priority

Major
Configure