Skip to content

Commit 1a94bcc

Browse files
NathanQingyangXudreab8
authored andcommitted
HHH-15258 Add test for issue
1 parent e29884b commit 1a94bcc

File tree

2 files changed

+297
-0
lines changed

2 files changed

+297
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package org.hibernate.orm.test.jpa.orphan.onetomany;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.hibernate.Hibernate;
7+
8+
import org.hibernate.testing.TestForIssue;
9+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
10+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
11+
import org.hibernate.testing.orm.junit.Jpa;
12+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
import jakarta.persistence.Entity;
17+
import jakarta.persistence.FetchType;
18+
import jakarta.persistence.GeneratedValue;
19+
import jakarta.persistence.GenerationType;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.ManyToOne;
22+
import jakarta.persistence.OneToMany;
23+
24+
import static org.hamcrest.CoreMatchers.is;
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
import static org.junit.jupiter.api.Assertions.assertSame;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
@Jpa(
30+
annotatedClasses = {
31+
EagerOneToManyOrphanWithIdentityIdTest.Parent.class,
32+
EagerOneToManyOrphanWithIdentityIdTest.Child.class
33+
}
34+
)
35+
@TestForIssue(jiraKey = "HHH-15258")
36+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsIdentityColumns.class)
37+
public class EagerOneToManyOrphanWithIdentityIdTest {
38+
39+
@AfterEach
40+
public void tearDown(EntityManagerFactoryScope scope) {
41+
scope.inTransaction(
42+
entityManager -> {
43+
entityManager.createQuery( "delete from Child" ).executeUpdate();
44+
entityManager.createQuery( "delete from Parent" ).executeUpdate();
45+
}
46+
);
47+
}
48+
49+
@Test
50+
public void testPersistParentAndQueryInTheSameSession(EntityManagerFactoryScope scope) {
51+
scope.inTransaction(
52+
entityManager -> {
53+
Parent parent = new Parent();
54+
entityManager.persist( parent );
55+
56+
List result = entityManager.createQuery( "from Parent" ).getResultList();
57+
58+
Parent p = (Parent) result.get( 0 );
59+
60+
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
61+
assertThat( p.getChildren().size(), is( 0 ) );
62+
63+
entityManager.createQuery( "from Parent" ).getResultList();
64+
}
65+
);
66+
}
67+
68+
@Test
69+
public void testPersistParentWithChildAndQueryInTheSameSession(EntityManagerFactoryScope scope) {
70+
scope.inTransaction(
71+
entityManager -> {
72+
Parent parent = new Parent();
73+
Child child = new Child();
74+
parent.addChild( child );
75+
entityManager.persist( child );
76+
entityManager.persist( parent );
77+
78+
List result = entityManager.createQuery( "from Parent" ).getResultList();
79+
80+
Parent p = (Parent) result.get( 0 );
81+
82+
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
83+
assertThat( p.getChildren().size(), is( 1 ) );
84+
assertSame( child, parent.getChildren().get( 0 ) );
85+
entityManager.createQuery( "from Parent" ).getResultList();
86+
}
87+
);
88+
}
89+
90+
@Entity(name = "Parent")
91+
static class Parent {
92+
93+
@Id
94+
@GeneratedValue(strategy = GenerationType.IDENTITY)
95+
Long id;
96+
97+
@OneToMany(mappedBy = "parent", orphanRemoval = true, fetch = FetchType.EAGER)
98+
List<Child> children = new ArrayList<>();
99+
100+
public Long getId() {
101+
return id;
102+
}
103+
104+
public void setId(Long id) {
105+
this.id = id;
106+
}
107+
108+
public List<Child> getChildren() {
109+
return children;
110+
}
111+
112+
public void setChildren(List<Child> children) {
113+
this.children = children;
114+
}
115+
116+
public void addChild(Child child) {
117+
children.add( child );
118+
child.setParent( this );
119+
}
120+
}
121+
122+
@Entity(name = "Child")
123+
static class Child {
124+
125+
@Id
126+
@GeneratedValue(strategy = GenerationType.IDENTITY)
127+
Long id;
128+
129+
@ManyToOne
130+
Parent parent;
131+
132+
public Long getId() {
133+
return id;
134+
}
135+
136+
public void setId(Long id) {
137+
this.id = id;
138+
}
139+
140+
public Parent getParent() {
141+
return parent;
142+
}
143+
144+
public void setParent(Parent parent) {
145+
this.parent = parent;
146+
}
147+
}
148+
149+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package org.hibernate.orm.test.jpa.orphan.onetomany;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.hibernate.Hibernate;
7+
8+
import org.hibernate.testing.TestForIssue;
9+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
10+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
11+
import org.hibernate.testing.orm.junit.Jpa;
12+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
import jakarta.persistence.Entity;
17+
import jakarta.persistence.GeneratedValue;
18+
import jakarta.persistence.GenerationType;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.ManyToOne;
21+
import jakarta.persistence.OneToMany;
22+
23+
import static org.hamcrest.CoreMatchers.is;
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.junit.jupiter.api.Assertions.assertSame;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
28+
@Jpa(
29+
annotatedClasses = {
30+
LazyOneToManyOrphanWithIdentityIdTest.Parent.class,
31+
LazyOneToManyOrphanWithIdentityIdTest.Child.class
32+
}
33+
)
34+
@TestForIssue(jiraKey = "HHH-15258")
35+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsIdentityColumns.class)
36+
public class LazyOneToManyOrphanWithIdentityIdTest {
37+
38+
@AfterEach
39+
public void tearDown(EntityManagerFactoryScope scope) {
40+
scope.inTransaction(
41+
entityManager -> {
42+
entityManager.createQuery( "delete from Child" ).executeUpdate();
43+
entityManager.createQuery( "delete from Parent" ).executeUpdate();
44+
}
45+
);
46+
}
47+
48+
@Test
49+
public void testPersistParentAndLoadInTheSameSession(EntityManagerFactoryScope scope) {
50+
scope.inTransaction(
51+
entityManager -> {
52+
Parent parent = new Parent();
53+
entityManager.persist( parent );
54+
55+
List result = entityManager.createQuery( "from Parent" ).getResultList();
56+
57+
Parent p = (Parent) result.get( 0 );
58+
59+
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
60+
assertThat( p.getChildren().size(), is( 0 ) );
61+
62+
entityManager.createQuery( "from Parent" ).getResultList();
63+
}
64+
);
65+
}
66+
67+
@Test
68+
public void testPersistParentWithChildAndLoadInTheSameSession(EntityManagerFactoryScope scope) {
69+
scope.inTransaction(
70+
entityManager -> {
71+
Parent parent = new Parent();
72+
Child child = new Child();
73+
parent.addChild( child );
74+
entityManager.persist( child );
75+
entityManager.persist( parent );
76+
77+
List result = entityManager.createQuery( "from Parent" ).getResultList();
78+
79+
Parent p = (Parent) result.get( 0 );
80+
81+
assertTrue( Hibernate.isInitialized( p.getChildren() ) );
82+
assertThat( p.getChildren().size(), is( 1 ) );
83+
assertSame( child, parent.getChildren().get( 0 ) );
84+
entityManager.createQuery( "from Parent" ).getResultList();
85+
}
86+
);
87+
}
88+
89+
@Entity(name = "Parent")
90+
static class Parent {
91+
92+
@Id
93+
@GeneratedValue(strategy = GenerationType.IDENTITY)
94+
Long id;
95+
96+
@OneToMany(mappedBy = "parent", orphanRemoval = true)
97+
List<Child> children = new ArrayList<>();
98+
99+
public Long getId() {
100+
return id;
101+
}
102+
103+
public void setId(Long id) {
104+
this.id = id;
105+
}
106+
107+
public List<Child> getChildren() {
108+
return children;
109+
}
110+
111+
public void setChildren(List<Child> children) {
112+
this.children = children;
113+
}
114+
115+
public void addChild(Child child) {
116+
children.add( child );
117+
child.setParent( this );
118+
}
119+
}
120+
121+
@Entity(name = "Child")
122+
static class Child {
123+
124+
@Id
125+
@GeneratedValue(strategy = GenerationType.IDENTITY)
126+
Long id;
127+
128+
@ManyToOne
129+
Parent parent;
130+
131+
public Long getId() {
132+
return id;
133+
}
134+
135+
public void setId(Long id) {
136+
this.id = id;
137+
}
138+
139+
public Parent getParent() {
140+
return parent;
141+
}
142+
143+
public void setParent(Parent parent) {
144+
this.parent = parent;
145+
}
146+
}
147+
148+
}

0 commit comments

Comments
 (0)