From ac4b674d5c40326c70c6742b60a08e51f1cd5a07 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Wed, 21 Jun 2023 10:39:58 +0200 Subject: [PATCH] HHH-16709 Add test for issue --- .../OrderUpdateNestedEmbeddedIdTest.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/orderupdates/OrderUpdateNestedEmbeddedIdTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/orderupdates/OrderUpdateNestedEmbeddedIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/orderupdates/OrderUpdateNestedEmbeddedIdTest.java new file mode 100644 index 0000000000..4da9bd06bb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/orderupdates/OrderUpdateNestedEmbeddedIdTest.java @@ -0,0 +1,140 @@ +/* + * 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.orderupdates; + +import java.io.Serializable; +import java.util.List; + +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +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.AfterAll; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; + +/** + * @author Marco Belladelli + */ +@SessionFactory +@DomainModel( annotatedClasses = { + OrderUpdateNestedEmbeddedIdTest.ParentId.class, + OrderUpdateNestedEmbeddedIdTest.Parent.class, + OrderUpdateNestedEmbeddedIdTest.Child1Id.class, + OrderUpdateNestedEmbeddedIdTest.Child1.class, + OrderUpdateNestedEmbeddedIdTest.Child2.class, +} ) +@ServiceRegistry( settings = @Setting( name = AvailableSettings.ORDER_UPDATES, value = "true" ) ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-16709" ) +public class OrderUpdateNestedEmbeddedIdTest { + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.createMutationQuery( "delete from Child2" ).executeUpdate(); + session.createMutationQuery( "delete from Child1" ).executeUpdate(); + session.createMutationQuery( "delete from Parent" ).executeUpdate(); + } ); + } + + @Test + public void testParentPersist(SessionFactoryScope scope) { + scope.inTransaction( session -> { + final ParentId parentId = new ParentId( "parent_1" ); + final Child1Id child1Id1 = new Child1Id( parentId, 1 ); + final Child1Id child1Id2 = new Child1Id( parentId, 2 ); + final Parent parent = new Parent( + parentId, + List.of( new Child1( child1Id1, List.of() ), new Child1( child1Id2, List.of( new Child2() ) ) ) + ); + session.persist( parent ); + } ); + } + + @Embeddable + public static class ParentId implements Serializable { + private String id; + + public ParentId() { + } + + public ParentId(String id) { + this.id = id; + } + } + + @Entity( name = "Parent" ) + public static class Parent { + @EmbeddedId + private ParentId parentId; + + @OneToMany( cascade = CascadeType.ALL ) + @JoinColumn( name = "parent_id", referencedColumnName = "id" ) + private List child1s; + + public Parent() { + } + + public Parent(ParentId parentId, List child1s) { + this.parentId = parentId; + this.child1s = child1s; + } + } + + @Embeddable + public static class Child1Id implements Serializable { + @Embedded + private ParentId parentId; + private Integer version; + + public Child1Id() { + } + + public Child1Id(ParentId parentId, Integer version) { + this.parentId = parentId; + this.version = version; + } + } + + @Entity( name = "Child1" ) + public static class Child1 { + @EmbeddedId + private Child1Id child1Id; + + @OneToMany( cascade = CascadeType.ALL ) + @JoinColumn( name = "child1_id", referencedColumnName = "id" ) + @JoinColumn( name = "child1_version", referencedColumnName = "version" ) + private List child2s; + + public Child1() { + } + + public Child1(Child1Id child1Id, List child2s) { + this.child1Id = child1Id; + this.child2s = child2s; + } + } + + @Entity( name = "Child2" ) + public static class Child2 { + @Id + @GeneratedValue + private Long id; + } +}