Skip to content

Commit 7c84a5b

Browse files
committed
HHH-18177 Add test for issue
1 parent 677c8b6 commit 7c84a5b

File tree

1 file changed

+106
-3
lines changed

1 file changed

+106
-3
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/merge/MergeUnsavedEntitiesTest.java

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
package org.hibernate.orm.test.bytecode.enhancement.merge;
22

33
import java.util.ArrayList;
4+
import java.util.HashSet;
45
import java.util.List;
6+
import java.util.Set;
57

68
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
79
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.Jira;
811
import org.hibernate.testing.orm.junit.JiraKey;
912
import org.hibernate.testing.orm.junit.SessionFactory;
1013
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.junit.jupiter.api.Test;
1115

1216
import jakarta.persistence.Entity;
1317
import jakarta.persistence.FetchType;
18+
import jakarta.persistence.GeneratedValue;
19+
import jakarta.persistence.GenerationType;
1420
import jakarta.persistence.Id;
21+
import jakarta.persistence.JoinColumn;
1522
import jakarta.persistence.ManyToOne;
1623
import jakarta.persistence.OneToMany;
1724
import jakarta.persistence.Table;
1825

1926
import static jakarta.persistence.CascadeType.MERGE;
2027
import static org.assertj.core.api.Assertions.assertThat;
2128

22-
import org.junit.jupiter.api.Test;
23-
2429

2530
@DomainModel(
2631
annotatedClasses = {
2732
MergeUnsavedEntitiesTest.Parent.class,
28-
MergeUnsavedEntitiesTest.Child.class
33+
MergeUnsavedEntitiesTest.Child.class,
34+
MergeUnsavedEntitiesTest.Book.class,
35+
MergeUnsavedEntitiesTest.BookNote.class,
2936
}
3037
)
3138
@SessionFactory
@@ -76,6 +83,43 @@ public void testMerge(SessionFactoryScope scope) {
7683
);
7784
}
7885

86+
@Test
87+
public void testMergeParentWithoutChildren(SessionFactoryScope scope) {
88+
scope.inTransaction(
89+
session -> {
90+
Parent parent = new Parent( 1l, 2l );
91+
session.merge( parent );
92+
}
93+
);
94+
95+
scope.inTransaction(
96+
session -> {
97+
Parent parent = session.find( Parent.class, 1l );
98+
assertThat( parent.getChildren()).isEmpty();
99+
}
100+
);
101+
}
102+
103+
@Test
104+
@Jira("HHH-18177")
105+
public void testMergeTransientInstanceWithGeneratedId(SessionFactoryScope scope) {
106+
Book merged = scope.fromTransaction(
107+
session -> {
108+
Book book = new Book( "9788806257231" );
109+
return session.merge( book );
110+
}
111+
);
112+
113+
scope.inTransaction(
114+
session -> {
115+
Book book = session.get( Book.class, merged.getId() );
116+
assertThat( book ).isNotNull();
117+
assertThat( book.getBookNotes() ).isEmpty();
118+
}
119+
);
120+
121+
}
122+
79123
@Entity(name = "Parent")
80124
@Table(name = "parent")
81125
public static class Parent {
@@ -171,4 +215,63 @@ public String getName() {
171215
}
172216
}
173217

218+
@Entity(name = "Book")
219+
public static class Book {
220+
221+
@Id
222+
@GeneratedValue(strategy = GenerationType.IDENTITY)
223+
private Long id;
224+
225+
private String isbn;
226+
227+
@OneToMany(mappedBy = "book", orphanRemoval = true, fetch = FetchType.LAZY)
228+
private Set<BookNote> bookNotes = new HashSet<>();
229+
230+
public Book() {
231+
}
232+
233+
public Book(String isbn) {
234+
this.isbn = isbn;
235+
}
236+
237+
public Long getId() {
238+
return id;
239+
}
240+
241+
public String getIsbn() {
242+
return isbn;
243+
}
244+
245+
public Set<BookNote> getBookNotes() {
246+
return bookNotes;
247+
}
248+
}
249+
250+
@Entity(name = "BookNote")
251+
public class BookNote {
252+
253+
@Id
254+
@GeneratedValue(strategy = GenerationType.IDENTITY)
255+
private Long id;
256+
257+
@ManyToOne(fetch = FetchType.LAZY)
258+
@JoinColumn(name = "BookID")
259+
private Book book;
260+
261+
private String note;
262+
263+
public Long getId() {
264+
return id;
265+
}
266+
267+
public Book getBook() {
268+
return book;
269+
}
270+
271+
public String getNote() {
272+
return note;
273+
}
274+
275+
}
276+
174277
}

0 commit comments

Comments
 (0)