|
| 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.collection; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.ManyToOne; |
| 11 | +import jakarta.persistence.OneToMany; |
| 12 | +import jakarta.persistence.Table; |
| 13 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 14 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +@DomainModel( |
| 26 | + annotatedClasses = { |
| 27 | + PersistAfterRemoveTest.Parent.class, |
| 28 | + PersistAfterRemoveTest.Child.class |
| 29 | + } |
| 30 | +) |
| 31 | +@SessionFactory |
| 32 | +@JiraKey(value = "HHH-4468") |
| 33 | +public class PersistAfterRemoveTest { |
| 34 | + |
| 35 | + private static final Integer PARENT_ID = 7242000; |
| 36 | + |
| 37 | + @BeforeAll |
| 38 | + public void prepare(SessionFactoryScope scope) { |
| 39 | + scope.inTransaction( session -> { |
| 40 | + Parent parent = new Parent( PARENT_ID, "Test" ); |
| 41 | + parent.addChild( new Child( 1 ) ); |
| 42 | + parent.addChild( new Child( 2 ) ); |
| 43 | + session.persist( parent ); |
| 44 | + } ); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void test(SessionFactoryScope scope) { |
| 49 | + scope.inTransaction( session -> { |
| 50 | + Parent foundParent = session.find( Parent.class, PARENT_ID ); |
| 51 | + session.remove( foundParent ); |
| 52 | + session.getTransaction().commit(); |
| 53 | + session.getTransaction().begin(); |
| 54 | + session.persist( foundParent ); |
| 55 | + } ); |
| 56 | + |
| 57 | + scope.inTransaction( session -> { |
| 58 | + Parent foundParent = session.find( Parent.class, PARENT_ID ); |
| 59 | + Set<Child> children = foundParent.getChildren(); |
| 60 | + assertThat( children.size() ).isEqualTo( 2 ); |
| 61 | + } ); |
| 62 | + } |
| 63 | + |
| 64 | + @Entity(name = "Parent") |
| 65 | + @Table(name = "parent_table") |
| 66 | + static class Parent { |
| 67 | + @Id |
| 68 | + private Integer id; |
| 69 | + private String name; |
| 70 | + |
| 71 | + @OneToMany(mappedBy="parent", cascade = CascadeType.ALL) |
| 72 | + private Set<Child> children = new HashSet<>(); |
| 73 | + |
| 74 | + public Parent() { |
| 75 | + } |
| 76 | + |
| 77 | + public Parent(Integer id, String name) { |
| 78 | + this.id = id; |
| 79 | + this.name = name; |
| 80 | + } |
| 81 | + |
| 82 | + public Integer getId() { |
| 83 | + return id; |
| 84 | + } |
| 85 | + |
| 86 | + public void setId(Integer id) { |
| 87 | + this.id = id; |
| 88 | + } |
| 89 | + |
| 90 | + public String getName() { |
| 91 | + return name; |
| 92 | + } |
| 93 | + |
| 94 | + public void setName(String name) { |
| 95 | + this.name = name; |
| 96 | + } |
| 97 | + |
| 98 | + public Set<Child> getChildren() { |
| 99 | + return children; |
| 100 | + } |
| 101 | + |
| 102 | + public void setChildren(Set<Child> children) { |
| 103 | + this.children = children; |
| 104 | + } |
| 105 | + |
| 106 | + public void addChild(Child child) { |
| 107 | + children.add( child ); |
| 108 | + child.parent = this; |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + @Entity(name = "Child") |
| 114 | + @Table(name = "child_table") |
| 115 | + public static class Child { |
| 116 | + |
| 117 | + @Id |
| 118 | + private Integer id; |
| 119 | + @ManyToOne |
| 120 | + private Parent parent; |
| 121 | + |
| 122 | + public Child() { |
| 123 | + } |
| 124 | + |
| 125 | + public Child(Integer id) { |
| 126 | + this.id = id; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments