{code:java}@Entity @Table(name = "MY_TABLE") @DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER) @DiscriminatorValue("0") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class ParentEntity {
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id;
@JoinColumn(name = "PARENT", foreignKey = @ForeignKey(name = "FK_PARENT_01")) @ManyToOne @Fetch(FetchMode.SELECT) ParentEntity parent; }{code}
{code:java}@Entity @DiscriminatorValue("1") public class ChildEntity extends ParentEntity {}{code}
{code:java}public class ORMUnitTestCase extends BaseCoreFunctionalTestCase {
// Add your entities here. @Override protected Class<?>[] getAnnotatedClasses() { return new Class<?>[] { ParentEntity.class, ChildEntity.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 hhh0000Test() throws Exception { // BaseCoreFunctionalTestCase automatically creates the SessionFactory and // provides the Session. ChildEntity child1 = new ChildEntity(); ChildEntity child2 = new ChildEntity(); try (Session s = openSession()) { Transaction tx = s.beginTransaction();
child1.parent = child2;
s.persist(child1); s.persist(child2); tx.commit();
// To simulate that all data was already persisted before the application starts s.getSessionFactory().getCache().evictAllRegions(); }
try (Session s = openSession()) { Transaction tx = s.beginTransaction(); ParentEntity entity = s.byId(ParentEntity.class).load(child1.id); assertThat(entity.parent).isInstanceOf(ChildEntity.class); tx.commit(); } } }{code}
{noformat}java.lang.AssertionError: Expecting: <org.hibernate.bugs.ChildEntity@713afa0e> to be an instance of: <org.hibernate.bugs.ChildEntity> but was instance of: <org.hibernate.bugs.ParentEntity$HibernateProxy$1AjGQHjF> at org.hibernate.bugs.ORMUnitTestCase.hhh0000Test(ORMUnitTestCase.java:79) 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){noformat}
The exact scenario works fine in 6.1.7.Final and 5.6.15
The issue only happens when all the conditions are met: "batch fetch size", "cache", "fetch mode select"
Reproducible test case to be uploaded attached and also available at [https://github.com/ratoaq2/HHH-16254|https://github.com/ratoaq2/HHH-16254|smart-link] |
|