|
| 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.detached; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 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 jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.FetchType; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.ManyToOne; |
| 23 | +import jakarta.persistence.OneToMany; |
| 24 | +import jakarta.persistence.OrderColumn; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +@DomainModel( |
| 29 | + annotatedClasses = { |
| 30 | + RemoveDetachedInstanceTest.Parent.class, |
| 31 | + RemoveDetachedInstanceTest.Child.class, |
| 32 | + RemoveDetachedInstanceTest.ParentChild.class |
| 33 | + } |
| 34 | +) |
| 35 | +@SessionFactory |
| 36 | +@BytecodeEnhanced(runNotEnhancedAsWell = true) |
| 37 | +@JiraKey("HHH-18631") |
| 38 | +public class RemoveDetachedInstanceTest { |
| 39 | + private static final Long PARENT_ID = 1L; |
| 40 | + private static final Long CHILD_ID = 2L; |
| 41 | + private static final Long PARENT_CHILD_ID = 3L; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setUp(SessionFactoryScope scope) { |
| 45 | + scope.inTransaction( session -> { |
| 46 | + Parent parent = new Parent( PARENT_ID, "parent" ); |
| 47 | + Child child = new Child( CHILD_ID, "child" ); |
| 48 | + ParentChild parentChild = new ParentChild( PARENT_CHILD_ID, parent, child ); |
| 49 | + |
| 50 | + session.persist( parent ); |
| 51 | + session.persist( child ); |
| 52 | + session.persist( parentChild ); |
| 53 | + } ); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterEach |
| 57 | + public void tearDown(SessionFactoryScope scope) { |
| 58 | + scope.inTransaction( session -> { |
| 59 | + session.createMutationQuery( "delete from ParentChild" ).executeUpdate(); |
| 60 | + session.createMutationQuery( "delete from Child" ).executeUpdate(); |
| 61 | + session.createMutationQuery( "delete from Parent" ).executeUpdate(); |
| 62 | + } ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testRemoveDetachedInstance(SessionFactoryScope scope) { |
| 67 | + ParentChild parentChild = scope.fromTransaction( session -> session.get( ParentChild.class, PARENT_CHILD_ID ) ); |
| 68 | + assertThat( parentChild ).isNotNull(); |
| 69 | + |
| 70 | + scope.inTransaction( session -> { |
| 71 | + session.remove( parentChild ); |
| 72 | + Parent parent = session.get( Parent.class, PARENT_ID ); |
| 73 | + assertThat( parent ).isNotNull(); |
| 74 | + List<ParentChild> pc = parent.getChildren(); |
| 75 | + assertThat( pc ).isNotNull(); |
| 76 | + assertThat( pc.size() ).isEqualTo( 1 ); |
| 77 | + assertThat( pc.get( 0 ) ).isSameAs( parentChild ); |
| 78 | + Child child = session.get( Child.class, CHILD_ID ); |
| 79 | + assertThat( child ).isNotNull(); |
| 80 | + } ); |
| 81 | + |
| 82 | + scope.inTransaction( session -> { |
| 83 | + ParentChild pc = session.get( ParentChild.class, PARENT_CHILD_ID ); |
| 84 | + assertThat( pc ).isNull(); |
| 85 | + Parent parent = session.get( Parent.class, PARENT_ID ); |
| 86 | + assertThat( parent ).isNotNull(); |
| 87 | + assertThat( parent.getChildren() ).isEmpty(); |
| 88 | + Child child = session.get( Child.class, CHILD_ID ); |
| 89 | + assertThat( child ).isNotNull(); |
| 90 | + assertThat( child.getChildren() ).isEmpty(); |
| 91 | + |
| 92 | + } ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testRemoveDetachedInstance2(SessionFactoryScope scope) { |
| 97 | + ParentChild parentChild = scope.fromTransaction( session -> session.get( ParentChild.class, PARENT_CHILD_ID ) ); |
| 98 | + assertThat( parentChild ).isNotNull(); |
| 99 | + |
| 100 | + scope.inTransaction( session -> { |
| 101 | + session.remove( parentChild ); |
| 102 | + session.remove( parentChild.getChild() ); |
| 103 | + Parent parent = session.get( Parent.class, PARENT_ID ); |
| 104 | + assertThat( parent ).isNotNull(); |
| 105 | + List<ParentChild> pc = parent.getChildren(); |
| 106 | + assertThat( pc ).isNotNull(); |
| 107 | + assertThat( pc.size() ).isEqualTo( 1 ); |
| 108 | + assertThat( pc.get( 0 ) ).isSameAs( parentChild ); |
| 109 | + Child child = session.get( Child.class, CHILD_ID ); |
| 110 | + assertThat( child ).isNull(); |
| 111 | + } ); |
| 112 | + |
| 113 | + scope.inTransaction( session -> { |
| 114 | + ParentChild pc = session.get( ParentChild.class, PARENT_CHILD_ID ); |
| 115 | + assertThat( pc ).isNull(); |
| 116 | + Parent parent = session.get( Parent.class, PARENT_ID ); |
| 117 | + assertThat( parent ).isNotNull(); |
| 118 | + assertThat( parent.getChildren() ).isEmpty(); |
| 119 | + Child child = session.get( Child.class, CHILD_ID ); |
| 120 | + assertThat( child ).isNull(); |
| 121 | + } ); |
| 122 | + } |
| 123 | + |
| 124 | + @Entity(name = "Parent") |
| 125 | + public static class Parent { |
| 126 | + @Id |
| 127 | + private Long id; |
| 128 | + |
| 129 | + private String name; |
| 130 | + |
| 131 | + @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER) |
| 132 | + List<ParentChild> children = new ArrayList<>(); |
| 133 | + |
| 134 | + public Parent() { |
| 135 | + } |
| 136 | + |
| 137 | + public Parent(Long id, String name) { |
| 138 | + this.id = id; |
| 139 | + this.name = name; |
| 140 | + } |
| 141 | + |
| 142 | + public Long getId() { |
| 143 | + return id; |
| 144 | + } |
| 145 | + |
| 146 | + public void setId(Long id) { |
| 147 | + this.id = id; |
| 148 | + } |
| 149 | + |
| 150 | + public List<ParentChild> getChildren() { |
| 151 | + return children; |
| 152 | + } |
| 153 | + |
| 154 | + public void setChildren(List<ParentChild> children) { |
| 155 | + this.children = children; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Entity(name = "ParentChild") |
| 160 | + public static class ParentChild { |
| 161 | + @Id |
| 162 | + private Long id; |
| 163 | + |
| 164 | + @ManyToOne |
| 165 | + private Parent parent; |
| 166 | + |
| 167 | + @ManyToOne |
| 168 | + private Child child; |
| 169 | + |
| 170 | + public ParentChild() { |
| 171 | + } |
| 172 | + |
| 173 | + public ParentChild(Long id, Parent parent, Child child) { |
| 174 | + this.id = id; |
| 175 | + this.parent = parent; |
| 176 | + this.child = child; |
| 177 | + parent.getChildren().add( this ); |
| 178 | + child.getChildren().add( this ); |
| 179 | + } |
| 180 | + |
| 181 | + public Long getId() { |
| 182 | + return id; |
| 183 | + } |
| 184 | + |
| 185 | + public Parent getParent() { |
| 186 | + return parent; |
| 187 | + } |
| 188 | + |
| 189 | + public Child getChild() { |
| 190 | + return child; |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + @Entity(name = "Child") |
| 196 | + public static class Child { |
| 197 | + |
| 198 | + @Id |
| 199 | + private Long id; |
| 200 | + |
| 201 | + private String name; |
| 202 | + |
| 203 | + @OneToMany(mappedBy = "child") |
| 204 | + @OrderColumn |
| 205 | + List<ParentChild> children = new ArrayList<>(); |
| 206 | + |
| 207 | + public Child() { |
| 208 | + } |
| 209 | + |
| 210 | + public Child(Long id, String name) { |
| 211 | + this.id = id; |
| 212 | + this.name = name; |
| 213 | + } |
| 214 | + |
| 215 | + public Long getId() { |
| 216 | + return id; |
| 217 | + } |
| 218 | + |
| 219 | + public void setId(Long id) { |
| 220 | + this.id = id; |
| 221 | + } |
| 222 | + |
| 223 | + public List<ParentChild> getChildren() { |
| 224 | + return children; |
| 225 | + } |
| 226 | + |
| 227 | + public void setChildren(List<ParentChild> children) { |
| 228 | + this.children = children; |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments