On our project we have entity with inheritance using join. After migration from hibernate 5 to 6 @SqlResultSetMapping doesn’t work.
Exception is following:
Caused by: java.lang.UnsupportedOperationException: null
at org.hibernate.metamodel.mapping.internal.CaseStatementDiscriminatorMappingImpl.getContainingTableExpression(CaseStatementDiscriminatorMappingImpl.java:210) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.results.complete.CompleteFetchBuilderBasicPart.buildFetch(CompleteFetchBuilderBasicPart.java:74) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.results.complete.CompleteResultBuilderEntityJpa.lambda$buildResult$1(CompleteResultBuilderEntityJpa.java:123) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.results.complete.EntityResultImpl.<init>(EntityResultImpl.java:81) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.results.complete.CompleteResultBuilderEntityJpa.buildResult(CompleteResultBuilderEntityJpa.java:113) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.results.complete.CompleteResultBuilderEntityJpa.buildResult(CompleteResultBuilderEntityJpa.java:38) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.results.ResultSetMappingImpl.resolve(ResultSetMappingImpl.java:222) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.resolveJdbcValuesSource(JdbcSelectExecutorStandardImpl.java:559) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:350) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:166) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:91) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.sql.internal.NativeSelectQueryPlanImpl.performList(NativeSelectQueryPlanImpl.java:105) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.sql.internal.NativeQueryImpl.doList(NativeQueryImpl.java:602) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at org.hibernate.query.Query.getResultList(Query.java:94) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:405) ~[spring-orm-6.0.11.jar:6.0.11]
at jdk.proxy2/jdk.proxy2.$Proxy87.getResultList(Unknown Source) ~[na:na]
at com.example.demo.AppStartupRunner.run(AppStartupRunner.java:23) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:760) ~[spring-boot-3.0.9.jar:3.0.9]
... 5 common frames omitted
Code of CaseStatementDiscriminatorMappingImpl.class looks suspicious:
@Override
public String getContainingTableExpression() {
throw new UnsupportedOperationException();
// // this *should* only be used to create the sql-expression key, so just
// // using the primary table expr should be fine
// return entityDescriptor.getRootTableName();
}
Test example to reproduce:
package com.example.demo.model;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* Parent entity.
*/
@Data
@EqualsAndHashCode(callSuper = false, of = "id")
@Entity
@Table(name = "parent")
@Inheritance(strategy = InheritanceType.JOINED)
public class Parent {
@Id
@Column(name = "id")
private Long id;
}
package com.example.demo.model;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* Child entity.
*/
@SqlResultSetMapping(
name = "ChildResult",
entities = {
@EntityResult(
entityClass = Child.class,
fields = {
@FieldResult(name = "id", column = "id"),
@FieldResult(name = "test", column = "test")
}
)
}
)
@NamedNativeQuery(
name = "Child",
query = "SELECT id, test FROM child",
resultSetMapping = "ChildResult"
)
@Data
@EqualsAndHashCode(callSuper = true, of = {})
@Entity
@Table(name = "child")
public class Child extends Parent {
@Column(name = "test")
private int test;
}
final List<Child> children =
entityManager.createNamedQuery("Child", Child.class).getResultList();
Reproducible on 6.1.7.Final and 6.2.7.Final.
Any suggestion how to solve the issue?