Skip to content

Commit cbca294

Browse files
committed
HHH-18713 HHH-18614 Add test to show issue does not persist with merge
1 parent bf5c499 commit cbca294

File tree

1 file changed

+240
-0
lines changed
  • hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge

1 file changed

+240
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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.bytecode.enhancement.merge;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.GeneratedValue;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.ManyToOne;
13+
import jakarta.persistence.OneToMany;
14+
import jakarta.persistence.Version;
15+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
16+
import org.hibernate.testing.orm.junit.DomainModel;
17+
import org.hibernate.testing.orm.junit.SessionFactory;
18+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.ArrayList;
23+
import java.util.LinkedHashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
27+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
28+
29+
@DomainModel(
30+
annotatedClasses = {
31+
MergeTest.Parent.class,
32+
MergeTest.Child.class,
33+
MergeTest.Owned.class,
34+
MergeTest.Owner.class,
35+
}
36+
)
37+
@SessionFactory
38+
@BytecodeEnhanced(runNotEnhancedAsWell = true)
39+
public class MergeTest {
40+
41+
@AfterEach
42+
public void tearDown(SessionFactoryScope scope) {
43+
scope.inTransaction(
44+
session -> {
45+
session.createQuery( "delete from Child" ).executeUpdate();
46+
session.createQuery( "delete from Parent" ).executeUpdate();
47+
}
48+
);
49+
}
50+
51+
@Test
52+
public void testMerge(SessionFactoryScope scope) {
53+
Parent parent = scope.fromTransaction(
54+
session -> {
55+
Parent p = new Parent( "a" );
56+
Child child = new Child( p, "b" );
57+
session.persist( p );
58+
return p;
59+
60+
}
61+
);
62+
63+
scope.inTransaction( session -> {
64+
Child child2 = new Child( parent, "c" );
65+
session.merge( parent );
66+
} );
67+
68+
scope.inTransaction( session -> {
69+
Parent saved = session.get( Parent.class, parent.getId() );
70+
assertThat( saved.getChildren().size() ).isEqualTo( 2 );
71+
} );
72+
}
73+
74+
@Test
75+
public void testMerge2(SessionFactoryScope scope) {
76+
Parent parent = scope.fromTransaction(
77+
session -> {
78+
Parent p = new Parent( "a" );
79+
Child child = new Child( p, "b" );
80+
session.persist( p );
81+
return p;
82+
83+
}
84+
);
85+
86+
scope.inTransaction( session -> {
87+
Child child2 = new Child( parent, "c" );
88+
session.merge( parent );
89+
} );
90+
91+
scope.inTransaction( session -> {
92+
Parent saved = session.get( Parent.class, parent.getId() );
93+
assertThat( saved.getChildren().size() ).isEqualTo( 2 );
94+
} );
95+
}
96+
97+
@Test
98+
void testMerge3(SessionFactoryScope scope) {
99+
Long ownerId = scope.fromTransaction( session -> {
100+
Owner owner = new Owner( "a" );
101+
owner.addOwned( new Owned() );
102+
session.persist( owner );
103+
return owner.getId();
104+
} );
105+
106+
scope.inTransaction( session -> {
107+
Owner owner2 = new Owner( ownerId, "a" );
108+
owner2.addOwned( new Owned() );
109+
session.merge( owner2 );
110+
session.flush();
111+
} );
112+
}
113+
114+
@Test
115+
void testMerge4(SessionFactoryScope scope) {
116+
Long ownerId = scope.fromTransaction( session -> {
117+
Owner owner = new Owner( );
118+
owner.addOwned( new Owned() );
119+
session.persist( owner );
120+
return owner.getId();
121+
} );
122+
123+
scope.inTransaction( session -> {
124+
Owner owner2 = new Owner( );
125+
owner2.id = ownerId;
126+
owner2.owneds.add( new Owned() );
127+
session.merge( owner2 );
128+
session.flush();
129+
} );
130+
}
131+
132+
@Entity(name = "Parent")
133+
public static class Parent {
134+
135+
@Id
136+
@GeneratedValue
137+
public Long id;
138+
139+
public Long getId() {
140+
return id;
141+
}
142+
143+
private String name;
144+
145+
@Version
146+
@Column(name = "VERSION_COLUMN")
147+
private long version;
148+
149+
public Parent() {
150+
}
151+
152+
public Parent(String name) {
153+
this.name = name;
154+
}
155+
156+
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
157+
private Set<Child> children = new LinkedHashSet<>();
158+
159+
public Set<Child> getChildren() {
160+
return children;
161+
}
162+
}
163+
164+
@Entity(name = "Child")
165+
public static class Child {
166+
167+
@Id
168+
@GeneratedValue
169+
public Long id;
170+
171+
public Long getId() {
172+
return id;
173+
}
174+
175+
@ManyToOne
176+
private Parent parent;
177+
178+
private String name;
179+
180+
public Child() {
181+
}
182+
183+
public Child(Parent parent, String name) {
184+
this.parent = parent;
185+
parent.children.add( this );
186+
this.name = name;
187+
}
188+
}
189+
190+
@Entity(name = "Owned")
191+
public static class Owned {
192+
193+
@Id
194+
@GeneratedValue
195+
private Long id;
196+
197+
private String name;
198+
199+
public Owned() {
200+
}
201+
202+
public Owned(String name) {
203+
this.name = name;
204+
}
205+
}
206+
207+
@Entity(name = "Owner")
208+
public static class Owner {
209+
210+
@Id
211+
@GeneratedValue
212+
private Long id;
213+
214+
private String name;
215+
216+
@OneToMany(cascade = CascadeType.ALL)
217+
private List<Owned> owneds = new ArrayList<>();
218+
219+
public Owner() {
220+
}
221+
222+
public Owner(String name) {
223+
this.name = name;
224+
}
225+
226+
public Owner(Long id, String name) {
227+
this.id = id;
228+
this.name = name;
229+
}
230+
231+
public void addOwned(Owned owned) {
232+
owneds.add( owned );
233+
}
234+
235+
public Long getId() {
236+
return id;
237+
}
238+
}
239+
240+
}

0 commit comments

Comments
 (0)