[JIRA] (HHH-16570) Batch fetch with FetchMode JOIN might lead to multiplication in OneToMany items
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiOWU3Y2M0NmM3... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16570?atlOrigin=eyJpIjoiOWU3Y2... ) HHH-16570 ( https://hibernate.atlassian.net/browse/HHH-16570?atlOrigin=eyJpIjoiOWU3Y2... ) Batch fetch with FetchMode JOIN might lead to multiplication in OneToMany items ( https://hibernate.atlassian.net/browse/HHH-16570?atlOrigin=eyJpIjoiOWU3Y2... )
Change By: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
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...]
( https://hibernate.atlassian.net/browse/HHH-16570#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16570#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:5ff5564 )
2 years, 11 months
[JIRA] (HHH-16570) Batch fetch with FetchMode JOIN might lead to multiplication in OneToMany items
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNWM4ZDM2M2Ux... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16570?atlOrigin=eyJpIjoiNWM4ZD... ) HHH-16570 ( https://hibernate.atlassian.net/browse/HHH-16570?atlOrigin=eyJpIjoiNWM4ZD... ) Batch fetch with FetchMode JOIN might lead to multiplication in OneToMany items ( https://hibernate.atlassian.net/browse/HHH-16570?atlOrigin=eyJpIjoiNWM4ZD... )
Issue Type: Bug Affects Versions: 6.2.2 Assignee: Unassigned Created: 08/May/2023 06:50 AM Priority: Critical Reporter: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
The following entity mapping configuration leads to incorrect results in EntityA.children
@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;
}
@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);
}
}
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)
The generated select SQLs:
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)
Failing test scenario to be attached
( https://hibernate.atlassian.net/browse/HHH-16570#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16570#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:5ff5564 )
2 years, 11 months