|
1 | 1 | package org.hibernate.orm.test.bytecode.enhancement.merge;
|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
| 4 | +import java.util.HashSet; |
4 | 5 | import java.util.List;
|
| 6 | +import java.util.Set; |
5 | 7 |
|
6 | 8 | import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
|
7 | 9 | import org.hibernate.testing.orm.junit.DomainModel;
|
| 10 | +import org.hibernate.testing.orm.junit.Jira; |
8 | 11 | import org.hibernate.testing.orm.junit.JiraKey;
|
9 | 12 | import org.hibernate.testing.orm.junit.SessionFactory;
|
10 | 13 | import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
| 14 | +import org.junit.jupiter.api.Test; |
11 | 15 |
|
12 | 16 | import jakarta.persistence.Entity;
|
13 | 17 | import jakarta.persistence.FetchType;
|
| 18 | +import jakarta.persistence.GeneratedValue; |
| 19 | +import jakarta.persistence.GenerationType; |
14 | 20 | import jakarta.persistence.Id;
|
| 21 | +import jakarta.persistence.JoinColumn; |
15 | 22 | import jakarta.persistence.ManyToOne;
|
16 | 23 | import jakarta.persistence.OneToMany;
|
17 | 24 | import jakarta.persistence.Table;
|
18 | 25 |
|
19 | 26 | import static jakarta.persistence.CascadeType.MERGE;
|
20 | 27 | import static org.assertj.core.api.Assertions.assertThat;
|
21 | 28 |
|
22 |
| -import org.junit.jupiter.api.Test; |
23 |
| - |
24 | 29 |
|
25 | 30 | @DomainModel(
|
26 | 31 | annotatedClasses = {
|
27 | 32 | MergeUnsavedEntitiesTest.Parent.class,
|
28 |
| - MergeUnsavedEntitiesTest.Child.class |
| 33 | + MergeUnsavedEntitiesTest.Child.class, |
| 34 | + MergeUnsavedEntitiesTest.Book.class, |
| 35 | + MergeUnsavedEntitiesTest.BookNote.class, |
29 | 36 | }
|
30 | 37 | )
|
31 | 38 | @SessionFactory
|
@@ -76,6 +83,43 @@ public void testMerge(SessionFactoryScope scope) {
|
76 | 83 | );
|
77 | 84 | }
|
78 | 85 |
|
| 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 | + |
79 | 123 | @Entity(name = "Parent")
|
80 | 124 | @Table(name = "parent")
|
81 | 125 | public static class Parent {
|
@@ -171,4 +215,63 @@ public String getName() {
|
171 | 215 | }
|
172 | 216 | }
|
173 | 217 |
|
| 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 | + |
174 | 277 | }
|
0 commit comments