From 9a94af1eb4f25eb6cf8fa594ac678f01d1ffb03e Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Tue, 30 May 2023 19:47:31 +0200 Subject: [PATCH] HHH-16725 Add a test case to reproduce the issue --- .../CompositeIdWithOrderedUpdatesTest.java | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/cid/CompositeIdWithOrderedUpdatesTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/cid/CompositeIdWithOrderedUpdatesTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/cid/CompositeIdWithOrderedUpdatesTest.java new file mode 100644 index 0000000000..e28c19ac6d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/cid/CompositeIdWithOrderedUpdatesTest.java @@ -0,0 +1,179 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.annotations.cid; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.IdClass; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; + +@DomainModel( + annotatedClasses = { + CompositeIdWithOrderedUpdatesTest.ModelWithSelfChildren.class + } +) +@ServiceRegistry( + settings = { + @Setting(name = AvailableSettings.ORDER_UPDATES, value = "true") + } +) +@SessionFactory +@JiraKey("HHH-16725") +public class CompositeIdWithOrderedUpdatesTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from ModelWithSelfChildren" ).executeUpdate(); + } + ); + } + + @Test + public void testSuccessfulPersist(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + ModelWithSelfChildren m = new ModelWithSelfChildren(); + m.setStringProperty( "a" ); + + session.persist( m ); + + ModelWithSelfChildren m2 = new ModelWithSelfChildren(); + m2.setStringProperty( "b" ); + + session.persist( m2 ); + } + ); + + scope.inTransaction( + session -> { + assertThat( + session.createQuery( "from ModelWithSelfChildren", ModelWithSelfChildren.class ) + .getResultList() + ).hasSize( 2 ); + } + ); + } + + @Entity(name = "ModelWithSelfChildren") + @IdClass(ModelWithSelfChildrenId.class) + public static class ModelWithSelfChildren { + + @Id + private String stringProperty; + @Id + private int integerProperty; + + @ManyToOne + private ModelWithSelfChildren parent; + + @OneToMany(mappedBy = "parent") + private List children = new ArrayList<>(); + + public String getStringProperty() { + return stringProperty; + } + + public void setStringProperty(String stringProperty) { + this.stringProperty = stringProperty; + } + + public int getIntegerProperty() { + return integerProperty; + } + + public void setIntegerProperty(int integerProperty) { + this.integerProperty = integerProperty; + } + + public ModelWithSelfChildren getParent() { + return parent; + } + + public void setParent(ModelWithSelfChildren parent) { + this.parent = parent; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + } + + public static class ModelWithSelfChildrenId implements Serializable { + + private String stringProperty; + private int integerProperty; + + + public String getStringProperty() { + return stringProperty; + } + + public void setStringProperty(String stringProperty) { + this.stringProperty = stringProperty; + } + + public int getIntegerProperty() { + return integerProperty; + } + + public void setIntegerProperty(int integerProperty) { + this.integerProperty = integerProperty; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + ModelWithSelfChildrenId that = (ModelWithSelfChildrenId) o; + return integerProperty == that.integerProperty && Objects.equals( stringProperty, that.stringProperty ); + } + + @Override + public int hashCode() { + return Objects.hash( stringProperty, integerProperty ); + } + + @Override + public String toString() { + return "Id{" + + "string='" + stringProperty + '\'' + + ", integer=" + integerProperty + + '}'; + } + } + + +}