From 0147541b1c6a97a939a1b6f9f7f3902c53966cbd Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 30 May 2023 15:03:07 +0200 Subject: [PATCH] HHH-16664 Add test for issue --- .../idclass/IdClassWithSuperclassTest.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idclass/IdClassWithSuperclassTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idclass/IdClassWithSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idclass/IdClassWithSuperclassTest.java new file mode 100644 index 0000000000..0180fc1929 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/idclass/IdClassWithSuperclassTest.java @@ -0,0 +1,90 @@ +package org.hibernate.orm.test.annotations.idclass; + +import java.io.Serializable; + +import org.hibernate.testing.orm.junit.DomainModel; +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.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.IdClass; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DomainModel( + annotatedClasses = { IdClassWithSuperclassTest.MyEntity.class } +) +@SessionFactory +@JiraKey("HHH-16664") +public class IdClassWithSuperclassTest { + + @Test + public void testSaveAndGet(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + MyEntity myEntity = new MyEntity( 1, 2 ); + session.persist( myEntity ); + } + ); + + scope.inTransaction( + session -> { + MyEntity myEntity = session.get( MyEntity.class, new ChildPrimaryKey( 1, 2 ) ); + assertThat( myEntity ).isNotNull(); + } + ); + } + + public static class ParentPrimaryKey implements Serializable { + private Integer parentId; + + public ParentPrimaryKey() { + } + + public ParentPrimaryKey(Integer parentId) { + this.parentId = parentId; + } + } + + public static class ChildPrimaryKey extends ParentPrimaryKey{ + private Integer childId; + + public ChildPrimaryKey() { + } + + public ChildPrimaryKey(Integer parentId, Integer childId) { + super(parentId); + this.childId = childId; + } + } + + @Entity + @IdClass(ChildPrimaryKey.class) + public static class MyEntity { + + @Id + private Integer parentId; + + @Id + private Integer childId; + + public MyEntity() { + } + + public MyEntity(Integer parentId, Integer childId) { + this.parentId = parentId; + this.childId = childId; + } + + public Integer getParentId() { + return parentId; + } + + public Integer getChildId() { + return childId; + } + } +}