HHH-953 / HHH-15667 Test cases for these issues

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2022-11-04 17:19:14 +01:00 committed by Jan Schatteman
parent fdae11f791
commit 58a4a4dd73
1 changed files with 139 additions and 0 deletions

View File

@ -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<User> 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<User> 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<User> users = session.createNamedQuery( "getIdWithoutUsingFieldResults", User.class ).getResultList();
Assertions.assertFalse( Hibernate.isPropertyInitialized( users.get(0), "principal" ) );
}
);
}
@Test
public void testGetIdUsingFieldResults() {
doInHibernate( this::sessionFactory,
session -> {
List<User> 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;
}
}
}