|
| 1 | +package org.hibernate.orm.test.jpa.orphan.onetomany; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.hibernate.Hibernate; |
| 7 | + |
| 8 | +import org.hibernate.testing.TestForIssue; |
| 9 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks; |
| 10 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 11 | +import org.hibernate.testing.orm.junit.Jpa; |
| 12 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import jakarta.persistence.Entity; |
| 17 | +import jakarta.persistence.FetchType; |
| 18 | +import jakarta.persistence.GeneratedValue; |
| 19 | +import jakarta.persistence.GenerationType; |
| 20 | +import jakarta.persistence.Id; |
| 21 | +import jakarta.persistence.ManyToOne; |
| 22 | +import jakarta.persistence.OneToMany; |
| 23 | + |
| 24 | +import static org.hamcrest.CoreMatchers.is; |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 28 | + |
| 29 | +@Jpa( |
| 30 | + annotatedClasses = { |
| 31 | + EagerOneToManyOrphanWithIdentityIdTest.Parent.class, |
| 32 | + EagerOneToManyOrphanWithIdentityIdTest.Child.class |
| 33 | + } |
| 34 | +) |
| 35 | +@TestForIssue(jiraKey = "HHH-15258") |
| 36 | +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsIdentityColumns.class) |
| 37 | +public class EagerOneToManyOrphanWithIdentityIdTest { |
| 38 | + |
| 39 | + @AfterEach |
| 40 | + public void tearDown(EntityManagerFactoryScope scope) { |
| 41 | + scope.inTransaction( |
| 42 | + entityManager -> { |
| 43 | + entityManager.createQuery( "delete from Child" ).executeUpdate(); |
| 44 | + entityManager.createQuery( "delete from Parent" ).executeUpdate(); |
| 45 | + } |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testPersistParentAndQueryInTheSameSession(EntityManagerFactoryScope scope) { |
| 51 | + scope.inTransaction( |
| 52 | + entityManager -> { |
| 53 | + Parent parent = new Parent(); |
| 54 | + entityManager.persist( parent ); |
| 55 | + |
| 56 | + List result = entityManager.createQuery( "from Parent" ).getResultList(); |
| 57 | + |
| 58 | + Parent p = (Parent) result.get( 0 ); |
| 59 | + |
| 60 | + assertTrue( Hibernate.isInitialized( p.getChildren() ) ); |
| 61 | + assertThat( p.getChildren().size(), is( 0 ) ); |
| 62 | + |
| 63 | + entityManager.createQuery( "from Parent" ).getResultList(); |
| 64 | + } |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testPersistParentWithChildAndQueryInTheSameSession(EntityManagerFactoryScope scope) { |
| 70 | + scope.inTransaction( |
| 71 | + entityManager -> { |
| 72 | + Parent parent = new Parent(); |
| 73 | + Child child = new Child(); |
| 74 | + parent.addChild( child ); |
| 75 | + entityManager.persist( child ); |
| 76 | + entityManager.persist( parent ); |
| 77 | + |
| 78 | + List result = entityManager.createQuery( "from Parent" ).getResultList(); |
| 79 | + |
| 80 | + Parent p = (Parent) result.get( 0 ); |
| 81 | + |
| 82 | + assertTrue( Hibernate.isInitialized( p.getChildren() ) ); |
| 83 | + assertThat( p.getChildren().size(), is( 1 ) ); |
| 84 | + assertSame( child, parent.getChildren().get( 0 ) ); |
| 85 | + entityManager.createQuery( "from Parent" ).getResultList(); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + @Entity(name = "Parent") |
| 91 | + static class Parent { |
| 92 | + |
| 93 | + @Id |
| 94 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 95 | + Long id; |
| 96 | + |
| 97 | + @OneToMany(mappedBy = "parent", orphanRemoval = true, fetch = FetchType.EAGER) |
| 98 | + List<Child> children = new ArrayList<>(); |
| 99 | + |
| 100 | + public Long getId() { |
| 101 | + return id; |
| 102 | + } |
| 103 | + |
| 104 | + public void setId(Long id) { |
| 105 | + this.id = id; |
| 106 | + } |
| 107 | + |
| 108 | + public List<Child> getChildren() { |
| 109 | + return children; |
| 110 | + } |
| 111 | + |
| 112 | + public void setChildren(List<Child> children) { |
| 113 | + this.children = children; |
| 114 | + } |
| 115 | + |
| 116 | + public void addChild(Child child) { |
| 117 | + children.add( child ); |
| 118 | + child.setParent( this ); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Entity(name = "Child") |
| 123 | + static class Child { |
| 124 | + |
| 125 | + @Id |
| 126 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 127 | + Long id; |
| 128 | + |
| 129 | + @ManyToOne |
| 130 | + Parent parent; |
| 131 | + |
| 132 | + public Long getId() { |
| 133 | + return id; |
| 134 | + } |
| 135 | + |
| 136 | + public void setId(Long id) { |
| 137 | + this.id = id; |
| 138 | + } |
| 139 | + |
| 140 | + public Parent getParent() { |
| 141 | + return parent; |
| 142 | + } |
| 143 | + |
| 144 | + public void setParent(Parent parent) { |
| 145 | + this.parent = parent; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments