Given
* a class hierarchy where all of them are concrete classes and mapped in a single table with discriminator, * 2 of them maps the same ManyToOne reference to the concrete parent class * Batch size should be enabled
When
* Loading entities from database
Then
* Hibernate batch loading is failing calling the wrong field setter for one of the child classes
{noformat}@Entity @Table(name = "MY_TABLE") @DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER) @DiscriminatorValue("0") public class ConcreteParentClass {
@Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "ID") private Integer id; }
@Entity @DiscriminatorValue("1") public class ChildA extends ConcreteParentClass { @JoinColumn(name = "REF_COL", foreignKey = @ForeignKey(name = "FK_REF_00")) @ManyToOne @Fetch(FetchMode.SELECT) ConcreteParentClass reference; }
@Entity @DiscriminatorValue("2") public class ChildB extends ConcreteParentClass {
@JoinColumn(name = "REF_COL", foreignKey = @ForeignKey(name = "FK_REF_00")) @ManyToOne @Fetch(FetchMode.SELECT) ConcreteParentClass reference; }
public class ORMUnitTestCase extends BaseCoreFunctionalTestCase { // Add your entities here. @Override protected Class<?>[] getAnnotatedClasses() { return new Class<?>[] { ConcreteParentClass.class, ChildA.class, ChildB.class, }; }
// Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. @Override protected void configure(Configuration configuration) { super.configure( configuration );
configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() ); configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() ); configuration.setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "10"); }
// Add your tests, using standard JUnit. @Test public void hhh123Test() throws Exception { // BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session. Session s = openSession(); Transaction tx = s.beginTransaction(); ChildA child1 = new ChildA(); ChildB child2 = new ChildB(); ChildA child3 = new ChildA(); ChildB child4 = new ChildB(); child1.reference = child2; child4.reference = child3; s.persist(child1); s.persist(child2); s.persist(child3); s.persist(child4); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Query<ConcreteParentClass> query = s.createQuery("from ConcreteParentClass", ConcreteParentClass.class); query.list(); tx.commit(); s.close(); } } jakarta.persistence.PersistenceException: Converting `org.hibernate.PropertyAccessException` to JPA `PersistenceException` : Could not set value of type [org.hibernate.bugs.ChildA] : `org.hibernate.bugs.ChildA.reference` (setter) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:165) at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:374) at org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:1073) at org.hibernate.bugs.ORMUnitTestCase.hhh123Test(ORMUnitTestCase.java:83) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299) at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: org.hibernate.PropertyAccessException: Could not set value of type [org.hibernate.bugs.ChildA] : `org.hibernate.bugs.ChildA.reference` (setter) at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:81) at org.hibernate.sql.results.graph.entity.internal.BatchEntitySelectFetchInitializer.setInstance(BatchEntitySelectFetchInitializer.java:105) at org.hibernate.sql.results.graph.entity.internal.BatchEntitySelectFetchInitializer.lambda$endLoading$1(BatchEntitySelectFetchInitializer.java:79) at java.base/java.util.HashMap.forEach(HashMap.java:1421) at org.hibernate.sql.results.graph.entity.internal.BatchEntitySelectFetchInitializer.endLoading(BatchEntitySelectFetchInitializer.java:73) at org.hibernate.sql.results.internal.StandardRowReader.finishUp(StandardRowReader.java:160) at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:223) at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33) at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:443) at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:166) at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:91) at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31) at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$0(ConcreteSqmSelectQueryPlan.java:113) at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:335) at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:276) at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:571) at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363) ... 17 more Caused by: java.lang.IllegalArgumentException: Can not set org.hibernate.bugs.ConcreteParentClass field org.hibernate.bugs.ChildA.reference to org.hibernate.bugs.ChildB at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75) at java.base/java.lang.reflect.Field.set(Field.java:799) at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:53) ... 33 more
{noformat}
This case works fine in hibernate 5.x (including the latest 5)
I simplified the mapping to have only enough to reproduce the bug. Moving the attribute mapping to a super class is not an option in my case due to some complex mapping hierarchy. I’m using a workaround creating my own PropertyAccessStrategy
I’ll attach a test case as soon I get a Jira issue number Testcase attached and also available at [https://github.com/ratoaq2/HHH-16248|https://github.com/ratoaq2/HHH-16248|smart-link] |
|