From 00e8506a131e6209018ddd4ad4f38b8020035397 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 19 May 2022 18:00:44 +0200 Subject: [PATCH] HHH-15286 Add test for issue --- .../idclass/IdClassWithOneAttributeTest.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/idclass/IdClassWithOneAttributeTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/idclass/IdClassWithOneAttributeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/idclass/IdClassWithOneAttributeTest.java new file mode 100644 index 0000000000..2628c43e8d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/idclass/IdClassWithOneAttributeTest.java @@ -0,0 +1,113 @@ +package org.hibernate.orm.test.idclass; + +import java.io.Serializable; + +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.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.IdClass; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@DomainModel( + annotatedClasses = IdClassWithOneAttributeTest.SystemUser.class +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-15286") +public class IdClassWithOneAttributeTest { + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + PK pk = new PK( "Linux" ); + SystemUser systemUser = new SystemUser(); + systemUser.setId( pk ); + systemUser.setName( "Andrea" ); + session.save( systemUser ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from SystemUser" ).executeUpdate() + ); + } + + @Test + public void testGet(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + PK pk = new PK( "Linux" ); + SystemUser systemUser = session.get( SystemUser.class, pk ); + assertNotNull(systemUser); + } + ); + } + + @Entity(name = "SystemUser") + @IdClass(PK.class) + public static class SystemUser { + + @Id + private String subsystem; + + private String name; + + public PK getId() { + return new PK( subsystem ); + } + + public void setId(PK id) { + this.subsystem = id.getSubsystem(); + } + + public String getSubsystem() { + return subsystem; + } + + public void setSubsystem(String subsystem) { + this.subsystem = subsystem; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + } + + public static class PK implements Serializable { + + private String subsystem; + + public PK(String subsystem) { + this.subsystem = subsystem; + } + + private PK() { + } + + public String getSubsystem() { + return subsystem; + } + + public void setSubsystem(String subsystem) { + this.subsystem = subsystem; + } + + } +}