From 68049bbf74c9ba402847d13618d6176800cec9ee Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Fri, 5 Apr 2024 16:42:45 +0200 Subject: [PATCH] HHH-17947 - Add test for issue Signed-off-by: Jan Schatteman --- .../orm/test/flush/HHH10445Test.java | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/flush/HHH10445Test.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/flush/HHH10445Test.java b/hibernate-core/src/test/java/org/hibernate/orm/test/flush/HHH10445Test.java new file mode 100644 index 0000000000..5a4915f9a8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/flush/HHH10445Test.java @@ -0,0 +1,141 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.flush; + +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Jan Schatteman + */ +@DomainModel( + annotatedClasses = { HHH10445Test.Person.class, HHH10445Test.Phone.class } +) +@SessionFactory +@JiraKey( value = "HHH-10445") +public class HHH10445Test { + + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> session.createMutationQuery( "delete from Person" ).executeUpdate() + ); + } + + @Test + @FailureExpected(jiraKey = "HHH-10445") + public void doTest(SessionFactoryScope scope) { + Person thePerson = scope.fromTransaction( + session -> { + Person p = new Person("John Doe"); + session.persist( p ); + return p; + } + ); + + scope.inTransaction( + session -> { + Phone phone = new Phone( "1234567890" ); + thePerson.addPhone( phone ); + session.persist( phone ); + Person person = session.createQuery( "select p from Person p", Person.class).getSingleResult(); + assertEquals(1, person.getPhones().size()); + } + ); + } + + @Entity(name = "Person") + public static class Person { + + @Id + @GeneratedValue + private Long id; + + private String name; + + @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) + private List phones = new ArrayList<>( ); + + public Person() {} + + public Person(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public List getPhones() { + return phones; + } + + public void addPhone(Phone phone) { + phones.add( phone ); + phone.setPerson( this ); + } + } + + @Entity(name = "Phone") + public static class Phone { + + @Id + @GeneratedValue + private Long id; + + @ManyToOne + private Person person; + + private String number; + + public Phone() { + } + + public Phone(String number) { + this.number = number; + } + + public Long getId() { + return id; + } + + public String getNumber() { + return number; + } + + public Person getPerson() { + return person; + } + + public void setPerson(Person person) { + this.person = person; + } + } + +}