From 036a37a7e8f246541021386e8bfef4e1862e5174 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 16 Sep 2022 11:13:09 +0200 Subject: [PATCH] HHH-15512 Add test for issue --- ...rsistAndQueryingInSameTransactionTest.java | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/PersistAndQueryingInSameTransactionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/PersistAndQueryingInSameTransactionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/PersistAndQueryingInSameTransactionTest.java new file mode 100644 index 0000000000..9567fdaf8c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/orphan/onetomany/PersistAndQueryingInSameTransactionTest.java @@ -0,0 +1,199 @@ +package org.hibernate.orm.test.jpa.orphan.onetomany; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.hibernate.annotations.Cascade; +import org.hibernate.annotations.CascadeType; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +@DomainModel( + annotatedClasses = { + PersistAndQueryingInSameTransactionTest.Parent.class, + PersistAndQueryingInSameTransactionTest.Child.class, + } +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-15512") +public class PersistAndQueryingInSameTransactionTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createMutationQuery( "delete from Child" ).executeUpdate(); + session.createMutationQuery( "delete from Parent" ).executeUpdate(); + } + ); + } + + @Test + public void testSelectingThePersistedEntity(SessionFactoryScope scope) { + String parentId = UUID.randomUUID().toString(); + scope.inTransaction( + session -> { + Child child = new Child(); + child.setName( "Andrew" ); + child.setAge( 5 ); + Parent parent = new Parent( parentId ); + parent.addChild( child ); + session.persist( parent ); + + session.createQuery( "select p from Parent p where p.id = :id", Parent.class ) + .setParameter( "id", parentId ) + .getSingleResult(); + + Parent p = session.find( Parent.class, parentId ); + assertThat( p, notNullValue() ); + + List children = p.getChildren(); + assertThat( children.size(), is( 1 ) ); + Child c = children.get( 0 ); + + assertThat( c.getAge(), equalTo( 5 ) ); + } + ); + + scope.inTransaction( + session -> { + Parent parent1 = session.find( Parent.class, parentId ); + assertThat( parent1, notNullValue() ); + + List children = parent1.getChildren(); + assertThat( children.size(), is( 1 ) ); + Child c = children.get( 0 ); + + assertThat( c.getAge(), equalTo( 5 ) ); + } + ); + } + + @Test + public void testSelectingAndModifying(SessionFactoryScope scope) { + String parentId = UUID.randomUUID().toString(); + scope.inTransaction( + session -> { + Child child = new Child(); + child.setName( "Andrew" ); + child.setAge( 5 ); + Parent parent = new Parent( parentId ); + parent.addChild( child ); + session.persist( parent ); + + Parent p = session.createQuery( "select p from Parent p where p.id = :id", Parent.class ) + .setParameter( "id", parentId ) + .getSingleResult(); + + p.getChildren().get( 0 ).setAge( 10 ); + + Parent parent1 = session.find( Parent.class, parentId ); + assertThat( parent1, notNullValue() ); + + List children = parent1.getChildren(); + assertThat( children.size(), is( 1 ) ); + Child c = children.get( 0 ); + + assertThat( c.getAge(), equalTo( 10 ) ); + } + ); + + scope.inTransaction( + session -> { + Parent parent1 = session.find( Parent.class, parentId ); + assertThat( parent1, notNullValue() ); + + List children = parent1.getChildren(); + assertThat( children.size(), is( 1 ) ); + Child c = children.get( 0 ); + + assertThat( c.getAge(), equalTo( 10 ) ); + } + ); + } + + @Entity(name = "Parent") + public static class Parent { + @Id + private String id; + + @Cascade(CascadeType.ALL) + @OneToMany(orphanRemoval = true, mappedBy = "parent") + private final List children = new ArrayList<>(); + + public Parent() { + } + + public Parent(String id) { + this.id = id; + } + + public String getId() { + return id; + } + + public void addChild(Child c) { + c.parent = this; + children.add( c ); + } + + public List getChildren() { + return children; + } + } + + @Entity(name = "Child") + public static class Child { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + private Integer age; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + private Parent parent; + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + } + +}