Skip to content

Commit e094b3c

Browse files
committed
HHH-18842 Add test for issue
1 parent 2036ef1 commit e094b3c

File tree

2 files changed

+417
-0
lines changed

2 files changed

+417
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.orphan.onetomany.merge;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.OneToMany;
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.JiraKey;
13+
import org.hibernate.testing.orm.junit.SessionFactory;
14+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.junit.jupiter.api.AfterEach;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
@DomainModel(
26+
annotatedClasses = {
27+
MergeCascadeWithMapCollectionTest.Parent.class,
28+
MergeCascadeWithMapCollectionTest.Child.class,
29+
}
30+
)
31+
@SessionFactory
32+
@JiraKey("HHH-18842")
33+
public class MergeCascadeWithMapCollectionTest {
34+
35+
private static final Long ID_PARENT_WITHOUT_CHILDREN = 1L;
36+
private static final Long ID_PARENT_WITH_CHILDREN = 2L;
37+
38+
@BeforeEach
39+
public void setUp(SessionFactoryScope scope) {
40+
scope.inTransaction(
41+
session -> {
42+
Parent parent = new Parent( ID_PARENT_WITHOUT_CHILDREN, "old name" );
43+
session.persist( parent );
44+
45+
Parent parent2 = new Parent( ID_PARENT_WITH_CHILDREN, "old name" );
46+
Child child = new Child( 2l, "Child" );
47+
parent2.addChild( child );
48+
49+
session.persist( child );
50+
session.persist( parent2 );
51+
}
52+
);
53+
}
54+
55+
@AfterEach
56+
private void tearDown(SessionFactoryScope scope) {
57+
scope.inTransaction(
58+
session -> {
59+
session.createMutationQuery( "delete from Parent" ).executeUpdate();
60+
session.createMutationQuery( "delete from Child" ).executeUpdate();
61+
}
62+
);
63+
}
64+
65+
@Test
66+
public void testMergeParentWihoutChildren(SessionFactoryScope scope) {
67+
scope.inTransaction(
68+
session -> {
69+
Parent parent = new Parent( ID_PARENT_WITHOUT_CHILDREN, "new name" );
70+
Parent merged = session.merge( parent );
71+
assertThat( merged.getName() ).isEqualTo( "new name" );
72+
}
73+
);
74+
}
75+
76+
@Test
77+
public void testMergeParentWithChildren(SessionFactoryScope scope) {
78+
scope.inTransaction(
79+
session -> {
80+
Parent parent = new Parent( ID_PARENT_WITH_CHILDREN, "new name" );
81+
Child child = new Child( 2l, "Child" );
82+
parent.addChild( child );
83+
Parent merged = session.merge( parent );
84+
assertThat( merged.getName() ).isEqualTo( "new name" );
85+
}
86+
);
87+
scope.inTransaction(
88+
session -> {
89+
Parent parent = session.get( Parent.class, ID_PARENT_WITH_CHILDREN );
90+
assertThat( parent.getChildren().size() ).isEqualTo( 1 );
91+
}
92+
);
93+
}
94+
95+
@Test
96+
public void testMergeParentWithChildren2(SessionFactoryScope scope) {
97+
scope.inTransaction(
98+
session -> {
99+
Parent parent = new Parent( ID_PARENT_WITH_CHILDREN, "new name" );
100+
session.merge( parent );
101+
}
102+
);
103+
scope.inTransaction(
104+
session -> {
105+
Parent parent = session.get( Parent.class, ID_PARENT_WITH_CHILDREN );
106+
assertThat( parent.getName() ).isEqualTo( "new name" );
107+
assertThat( parent.getChildren().size() ).isEqualTo( 0 );
108+
109+
List<Child> children = session.createQuery( "Select c from Child c", Child.class ).list();
110+
assertThat( children.size() ).isEqualTo( 1 );
111+
}
112+
);
113+
}
114+
115+
@Entity(name = "Parent")
116+
public static class Parent {
117+
118+
@Id
119+
private Long id;
120+
121+
private String name;
122+
123+
@OneToMany(cascade = CascadeType.MERGE)
124+
private Map<String, Child> children;
125+
126+
public Parent() {
127+
}
128+
129+
public Parent(Long id, String name) {
130+
this.id = id;
131+
this.name = name;
132+
}
133+
134+
public Long getId() {
135+
return id;
136+
}
137+
138+
public void setId(Long id) {
139+
this.id = id;
140+
}
141+
142+
public String getName() {
143+
return name;
144+
}
145+
146+
public void setName(String name) {
147+
this.name = name;
148+
}
149+
150+
public Map<String, Child> getChildren() {
151+
return children;
152+
}
153+
154+
public void setChildren(Map<String, Child> children) {
155+
this.children = children;
156+
}
157+
158+
public void addChild(Child child) {
159+
if ( children == null ) {
160+
children = new HashMap<>();
161+
}
162+
children.put( child.getName(), child );
163+
}
164+
}
165+
166+
@Entity(name = "Child")
167+
public static class Child {
168+
169+
@Id
170+
private Long id;
171+
172+
private String name;
173+
174+
public Child() {
175+
}
176+
177+
public Child(Long id, String name) {
178+
this.id = id;
179+
this.name = name;
180+
}
181+
182+
public Long getId() {
183+
return id;
184+
}
185+
186+
public void setId(Long id) {
187+
this.id = id;
188+
}
189+
190+
public String getName() {
191+
return name;
192+
}
193+
194+
public void setName(String name) {
195+
this.name = name;
196+
}
197+
198+
}
199+
200+
}

0 commit comments

Comments
 (0)