The following entity mapping configuration leads to incorrect results in {{EntityA.children}}
{code:java}@Entity @Table(name = "ENTITY_A") public class EntityA { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id;
@JoinColumn(name = "PARENT") @ManyToOne EntityA parent;
@OneToMany(mappedBy = "parent") List<EntityA> children = new ArrayList<>();
@JoinColumn(name = "ENTITY_A") @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL) @Fetch(FetchMode.JOIN) List<EntityB> listOfEntitiesB = new ArrayList<>(); }
@Entity @Table(name = "ENTITY_B") public class EntityB { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") Integer id; }{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"); } @Test public void hhhXXXXTest() throws Exception { try (Session s = openSession()) { Transaction tx = s.beginTransaction(); EntityA entityA = new EntityA(); EntityA childA1 = new EntityA(); EntityA childA2 = new EntityA(); EntityB entityB1 = new EntityB(); EntityB entityB2 = new EntityB(); EntityB entityB3 = new EntityB();
entityA.children.addAll(List.of(childA1, childA2));
childA1.parent = entityA; childA1.listOfEntitiesB.addAll(List.of(entityB1, entityB2, entityB3));
childA2.parent = entityA;
s.persist(entityA); s.persist(childA1); s.persist(childA2); s.persist(entityB1); s.persist(entityB2); s.persist(entityB3); tx.commit(); }
try (Session s = openSession()) { List<EntityA> entitiesA = s.createQuery("select a from EntityA a where a.parent is null", EntityA.class) .getResultList(); assertThat(entitiesA).hasSize(1); assertThat(entitiesA.get(0).children).hasSize(2); } }{code}
{noformat}java.lang.AssertionError: Expected size:<2> but was:<4> in: <[org.hibernate.bugs.EntityA@46eecf54, org.hibernate.bugs.EntityA@46eecf54, org.hibernate.bugs.EntityA@46eecf54, org.hibernate.bugs.EntityA@2736259a]> at org.hibernate.bugs.ORMUnitTestCase.hhhXXXXTest(ORMUnitTestCase.java:91) 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 generated select SQLs:
{code:sql}Hibernate: select e1_0.ID, e1_0.PARENT from ENTITY_A e1_0 where e1_0.PARENT is null Hibernate: select l1_0.ENTITY_A, l1_0.ID from ENTITY_B l1_0 where array_contains(?,l1_0.ENTITY_A) Hibernate: select c1_0.PARENT, c1_0.ID, l1_0.ENTITY_A, l1_0.ID from ENTITY_A c1_0 left join ENTITY_B l1_0 on c1_0.ID=l1_0.ENTITY_A where array_contains(?,c1_0.PARENT){code}
Failing test scenario to be attached and also available at [https://github.com/ratoaq2/HHH-16570|https://github.com/ratoaq2/HHH-16570|smart-link] |
|