{code:java}@Entity @Table(name = "ENTITY_A") public class EntityA { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id;
@Column(name = "FOO") Integer foo;
@JoinColumn(name = "CURRENT_ENTITY_B") @ManyToOne EntityB currentEntityB;
// not needed for bug report // @OneToMany(mappedBy = "entityA") // List<EntityB> listOfEntitiesB = new ArrayList<>(); }
@Entity @Table(name = "ENTITY_B") public class EntityB { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id;
@JoinColumn(name = "ENTITY_A") @ManyToOne EntityA entityA; }{code}
{code:java} @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"); configuration.setProperty(HibernateHints.HINT_FLUSH_MODE, FlushMode.COMMIT.name()); } @Test public void hhhXXXXTest() throws Exception { Integer currentBId; try (Session s = openSession()) { Transaction tx = s.beginTransaction(); EntityA entityA1 = new EntityA(); EntityB entityB1 = new EntityB();
entityB1.entityA = entityA1; entityA1.currentEntityB = entityB1;
s.persist(entityA1); s.persist(entityB1); s.flush();
entityA1.foo = 123; EntityB entityB2 = new EntityB(); entityB2.entityA = entityA1; entityA1.currentEntityB = entityB2; s.persist(entityB2); currentBId = entityB2.id;
List<EntityA> entitiesA = s.createQuery("select a from EntityA a", EntityA.class).getResultList(); assertThat(entitiesA).hasSize(1); tx.commit(); }
try (Session s = openSession()) { List<EntityA> entitiesA = s.createQuery("select a from EntityA a", EntityA.class).getResultList();
assertThat(entitiesA).hasSize(1); assertThat(entitiesA.get(0).foo).isEqualTo(123); assertThat(entitiesA.get(0).currentEntityB.id).isEqualTo(currentBId); } }{code}
{noformat}org.opentest4j.AssertionFailedError: Expecting: <1> to be equal to: <2> but was not. at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at org.hibernate.bugs.ORMUnitTestCase.hhhXXXXTest(ORMUnitTestCase.java:98) 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}
of course, disabling batch fetch or switching to default flush mode would avoid the issue.
The problem happens because the first query loads the {{EntityA.currentEntityB}} in a batch load which will use {{EntityB.entityA}} to override the parentInstance property.
Failing test scenario to be attached and also available at [https://github.com/ratoaq2/HHH-16575|https://github.com/ratoaq2/HHH-16575|smart-link] |
|