|
| 1 | +package org.hibernate.orm.test.jpa.callbacks; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hibernate.annotations.Fetch; |
| 7 | +import org.hibernate.annotations.FetchMode; |
| 8 | + |
| 9 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.Jpa; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import jakarta.persistence.CascadeType; |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.FetchType; |
| 18 | +import jakarta.persistence.Id; |
| 19 | +import jakarta.persistence.IdClass; |
| 20 | +import jakarta.persistence.JoinColumn; |
| 21 | +import jakarta.persistence.JoinColumns; |
| 22 | +import jakarta.persistence.ManyToOne; |
| 23 | +import jakarta.persistence.OneToMany; |
| 24 | +import jakarta.persistence.PrePersist; |
| 25 | + |
| 26 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 27 | + |
| 28 | + |
| 29 | +@Jpa( |
| 30 | + annotatedClasses = { |
| 31 | + PrePersistAndCompositeIdTest.Parent.class, |
| 32 | + PrePersistAndCompositeIdTest.Child.class, |
| 33 | + } |
| 34 | +) |
| 35 | +@JiraKey("HHH-18032") |
| 36 | +public class PrePersistAndCompositeIdTest { |
| 37 | + |
| 38 | + private static final Long PARENT_ID1 = 1L; |
| 39 | + private static final Long PARENT_ID2 = 2L; |
| 40 | + private static final Long PARENT_ID_2_2 = 3L; |
| 41 | + |
| 42 | + |
| 43 | + private static final Long CHILD_ID1 = 3L; |
| 44 | + private static final Long CHILD_ID2 = 4L; |
| 45 | + private static final Long CHILD_ID_2_2 = 5L; |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 49 | + scope.inTransaction( |
| 50 | + entityManager -> { |
| 51 | + entityManager.createQuery( "delete from Child" ).executeUpdate(); |
| 52 | + entityManager.createQuery( "delete from Parent" ).executeUpdate(); |
| 53 | + } |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testMerge(EntityManagerFactoryScope scope) { |
| 59 | + scope.inTransaction( |
| 60 | + entityManager -> { |
| 61 | + Child child = new Child( CHILD_ID1, null, "child" ); |
| 62 | + Child merged = entityManager.merge( child ); |
| 63 | + Parent parent = new Parent( PARENT_ID1, null, "parent", merged ); |
| 64 | + entityManager.merge( parent ); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + scope.inTransaction( |
| 69 | + session -> { |
| 70 | + Parent parent = session.find( Parent.class, new CompositeId( PARENT_ID1, PARENT_ID2 ) ); |
| 71 | + assertThat( parent ).isNotNull(); |
| 72 | + assertThat( parent.getChildren().size() ).isEqualTo( 1 ); |
| 73 | + } |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testPersist(EntityManagerFactoryScope scope) { |
| 79 | + scope.inTransaction( |
| 80 | + entityManager -> { |
| 81 | + Child child = new Child( CHILD_ID1, null, "child" ); |
| 82 | + entityManager.persist( child ); |
| 83 | + Parent parent = new Parent( PARENT_ID1, null, "parent", child ); |
| 84 | + entityManager.persist( parent ); |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + scope.inTransaction( |
| 89 | + session -> { |
| 90 | + Parent parent = session.find( Parent.class, new CompositeId( PARENT_ID1, PARENT_ID2 ) ); |
| 91 | + assertThat( parent ).isNotNull(); |
| 92 | + assertThat( parent.getChildren().size() ).isEqualTo( 1 ); |
| 93 | + } |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testMergeAssignBothId(EntityManagerFactoryScope scope) { |
| 99 | + |
| 100 | + scope.inTransaction( |
| 101 | + entityManager -> { |
| 102 | + Child child = new Child( CHILD_ID1, CHILD_ID_2_2, "child" ); |
| 103 | + Child merged = entityManager.merge( child ); |
| 104 | + Parent parent = new Parent( PARENT_ID1, PARENT_ID_2_2, "parent", merged ); |
| 105 | + entityManager.merge( parent ); |
| 106 | + } |
| 107 | + ); |
| 108 | + scope.inTransaction( |
| 109 | + session -> { |
| 110 | + Parent parent = session.find( Parent.class, new CompositeId( PARENT_ID1, PARENT_ID_2_2 ) ); |
| 111 | + assertThat( parent ).isNotNull(); |
| 112 | + assertThat( parent.getChildren().size() ).isEqualTo( 1 ); |
| 113 | + } |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testPersistAssignBothId(EntityManagerFactoryScope scope) { |
| 119 | + scope.inTransaction( |
| 120 | + entityManager -> { |
| 121 | + Child child = new Child( CHILD_ID1, CHILD_ID_2_2, "child" ); |
| 122 | + entityManager.persist( child ); |
| 123 | + Parent parent = new Parent( PARENT_ID1, PARENT_ID_2_2, "parent", child ); |
| 124 | + entityManager.persist( parent ); |
| 125 | + } |
| 126 | + ); |
| 127 | + scope.inTransaction( |
| 128 | + session -> { |
| 129 | + Parent parent = session.find( Parent.class, new CompositeId( PARENT_ID1, PARENT_ID_2_2 ) ); |
| 130 | + assertThat( parent ).isNotNull(); |
| 131 | + assertThat( parent.getChildren().size() ).isEqualTo( 1 ); |
| 132 | + } |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + @Entity(name = "Parent") |
| 137 | + @IdClass(CompositeId.class) |
| 138 | + public static class Parent { |
| 139 | + @Id |
| 140 | + private Long id1; |
| 141 | + |
| 142 | + @Id |
| 143 | + private Long id2; |
| 144 | + |
| 145 | + private String name; |
| 146 | + |
| 147 | + @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) |
| 148 | + @Fetch(FetchMode.SELECT) |
| 149 | + List<Child> children = new ArrayList<>(); |
| 150 | + |
| 151 | + public Parent() { |
| 152 | + } |
| 153 | + |
| 154 | + public Parent(Long id1, Long id2, String name, Child child) { |
| 155 | + this.id1 = id1; |
| 156 | + this.id2 = id2; |
| 157 | + this.name = name; |
| 158 | + this.children.add( child ); |
| 159 | + child.parent = this; |
| 160 | + } |
| 161 | + |
| 162 | + @PrePersist |
| 163 | + public void prePersist() { |
| 164 | + if ( id2 == null ) { |
| 165 | + id2 = PARENT_ID2; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public Long getId1() { |
| 170 | + return id1; |
| 171 | + } |
| 172 | + |
| 173 | + public Long getId2() { |
| 174 | + return id2; |
| 175 | + } |
| 176 | + |
| 177 | + public String getName() { |
| 178 | + return name; |
| 179 | + } |
| 180 | + |
| 181 | + public List<Child> getChildren() { |
| 182 | + return children; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Entity(name = "Child") |
| 187 | + @IdClass(CompositeId.class) |
| 188 | + public static class Child { |
| 189 | + @Id |
| 190 | + private Long id1; |
| 191 | + |
| 192 | + @Id |
| 193 | + private Long id2; |
| 194 | + |
| 195 | + private String name; |
| 196 | + |
| 197 | + @ManyToOne |
| 198 | + @JoinColumns({ |
| 199 | + @JoinColumn(name = "parent_id1", referencedColumnName = "id1"), |
| 200 | + @JoinColumn(name = "parent_id2", referencedColumnName = "id2") |
| 201 | + }) |
| 202 | + private Parent parent; |
| 203 | + |
| 204 | + public Child() { |
| 205 | + } |
| 206 | + |
| 207 | + public Child(Long id1, Long id2, String name) { |
| 208 | + this.id1 = id1; |
| 209 | + this.id2 = id2; |
| 210 | + this.name = name; |
| 211 | + } |
| 212 | + |
| 213 | + @PrePersist |
| 214 | + public void prePersist() { |
| 215 | + if ( id2 == null ) { |
| 216 | + id2 = CHILD_ID2; |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + public static class CompositeId { |
| 222 | + private Long id1; |
| 223 | + |
| 224 | + private Long id2; |
| 225 | + |
| 226 | + public CompositeId() { |
| 227 | + } |
| 228 | + |
| 229 | + public CompositeId(Long id1, Long id2) { |
| 230 | + this.id1 = id1; |
| 231 | + this.id2 = id2; |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments