From 58a4a4dd73fe0660f9d6f02e573328d275dfefc7 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Fri, 4 Nov 2022 17:19:14 +0100 Subject: [PATCH] HHH-953 / HHH-15667 Test cases for these issues Signed-off-by: Jan Schatteman --- .../lazy/LazySqlResultMappingTest.java | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazySqlResultMappingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazySqlResultMappingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazySqlResultMappingTest.java new file mode 100644 index 0000000000..b93a6edf7d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bytecode/enhancement/lazy/LazySqlResultMappingTest.java @@ -0,0 +1,139 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.bytecode.enhancement.lazy; + +import java.util.List; + +import org.hibernate.Hibernate; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.orm.junit.JiraKey; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.runner.RunWith; + +import jakarta.persistence.Basic; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityResult; +import jakarta.persistence.FetchType; +import jakarta.persistence.FieldResult; +import jakarta.persistence.Id; +import jakarta.persistence.NamedNativeQuery; +import jakarta.persistence.SqlResultSetMapping; +import jakarta.persistence.Table; + +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Jan Schatteman + * @author Christian Beikov + */ +@RunWith( BytecodeEnhancerRunner.class ) +public class LazySqlResultMappingTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[]{ User.class }; + } + + @Before + public void prepareData() { + doInHibernate( this::sessionFactory, + session -> { + session.persist(new User(1L, (byte)1)); + } + ); + } + + @After + public void cleanupData() { + doInHibernate( this::sessionFactory, + session -> { + session.createMutationQuery("delete from User").executeUpdate(); + } + ); + } + + @Test + public void testGetIdAndPrincipalUsingFieldResults() { + doInHibernate( this::sessionFactory, + session -> { + List users = session.createNamedQuery( "getIdAndPrincipalUsingFieldResults", User.class ).getResultList(); + Assertions.assertTrue( Hibernate.isPropertyInitialized( users.get(0), "principal" ) ); + } + ); + } + + @Test + @JiraKey( value = "HHH-953" ) + public void testGetIdAndPrincipalWithoutUsingFieldResults() { + doInHibernate( this::sessionFactory, + session -> { + List users = session.createNamedQuery( "getIdAndPrincipalWithoutUsingFieldResults", User.class ).getResultList(); + Assertions.assertTrue( Hibernate.isPropertyInitialized( users.get(0), "principal" ) ); + } + ); + } + + @Test + @JiraKey( value = "HHH-15667" ) + @FailureExpected(jiraKey = "HHH-15667", message = "SQLGrammarException: Unable to find column position by name: principal") + public void testGetIdWithoutUsingFieldResults() { + doInHibernate( this::sessionFactory, + session -> { + List users = session.createNamedQuery( "getIdWithoutUsingFieldResults", User.class ).getResultList(); + Assertions.assertFalse( Hibernate.isPropertyInitialized( users.get(0), "principal" ) ); + } + ); + } + + @Test + public void testGetIdUsingFieldResults() { + doInHibernate( this::sessionFactory, + session -> { + List users = session.createNamedQuery( "getIdUsingFieldResults", User.class ).getResultList(); + Assertions.assertFalse( Hibernate.isPropertyInitialized( users.get(0), "principal" ) ); + } + ); + } + + @NamedNativeQuery(name = "getIdAndPrincipalUsingFieldResults", query = "select u.id as id, u.principal as principal from user_tbl u", resultSetMapping = "id_and_principal_with_fields") + @NamedNativeQuery(name = "getIdUsingFieldResults", query = "select u.id as id from user_tbl u", resultSetMapping = "id_with_fields") + @NamedNativeQuery(name = "getIdAndPrincipalWithoutUsingFieldResults", query = "select u.id as id, u.principal as principal from user_tbl u", resultSetMapping = "without_fields") + @NamedNativeQuery(name = "getIdWithoutUsingFieldResults", query = "select u.id as id from user_tbl u", resultSetMapping = "without_fields") + + @SqlResultSetMapping( name = "id_and_principal_with_fields", + entities = @EntityResult( entityClass = User.class, fields = { @FieldResult(name = "id", column = "id"), @FieldResult(name = "principal", column = "principal") } ) + ) + @SqlResultSetMapping( name = "id_with_fields", + entities = @EntityResult( entityClass = User.class, fields = { @FieldResult(name = "id", column = "id") } ) + ) + @SqlResultSetMapping( name = "without_fields", + entities = @EntityResult( entityClass = User.class ) + ) + + @Entity( name = "User") + @Table(name = "user_tbl") + private static class User { + @Id + private Long id; + @Basic(fetch = FetchType.LAZY) + private Byte principal; + + public User() { + } + + public User(Long id, Byte principal) { + this.id = id; + this.principal = principal; + } + } + +}