From 61eadfcabdbb35d5a4f19a0cde9446566df60458 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Wed, 9 Jul 2014 12:54:15 +0200 Subject: [PATCH] HHH-8237 Applying type configured via @ColumnMapping#type() for constructor results --- .../ResultsetMappingSecondPass.java | 2 +- .../ConstructorResultNativeQueryTest.java | 58 +++++++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/ResultsetMappingSecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/ResultsetMappingSecondPass.java index e2c3c2d771..9880f32d0c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/ResultsetMappingSecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/ResultsetMappingSecondPass.java @@ -200,7 +200,7 @@ public class ResultsetMappingSecondPass implements QuerySecondPass { columnReturns.add( new NativeSQLQueryScalarReturn( mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( columnResult.name() ), - null + columnResult.type() != null ? mappings.getTypeResolver().heuristicType( columnResult.type().getName() ) : null ) ); } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/ConstructorResultNativeQueryTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/ConstructorResultNativeQueryTest.java index a7ab1b9ba6..b64341a2cb 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/ConstructorResultNativeQueryTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/ConstructorResultNativeQueryTest.java @@ -23,8 +23,12 @@ */ package org.hibernate.jpa.test.query; +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; +import static org.junit.Assert.assertEquals; + import java.util.Date; import java.util.List; + import javax.persistence.Column; import javax.persistence.ColumnResult; import javax.persistence.ConstructorResult; @@ -39,13 +43,8 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.junit.Test; -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertEquals; - /** * @author Steve Ebersole */ @@ -83,6 +82,19 @@ public class ConstructorResultNativeQueryTest extends BaseEntityManagerFunctiona } ) } + ), + @SqlResultSetMapping( + name = "person-id-and-name-and-weight", + classes = { + @ConstructorResult( + targetClass = Person.class, + columns = { + @ColumnResult( name = "id" ), + @ColumnResult( name = "p_name" ), + @ColumnResult( name = "p_weight", type=String.class ) + } + ) + } ) } ) @@ -97,6 +109,11 @@ public class ConstructorResultNativeQueryTest extends BaseEntityManagerFunctiona name = "person-id-and-name2", query = "select p.id, p.p_name, p.id as id2, p.p_name as p_name2 from person p order by p.p_name", resultSetMapping = "person-id-and-name2" + ), + @NamedNativeQuery( + name = "person-id-and-name-and-weight", + query = "select p.id, p.p_name, p.p_weight from person p order by p.p_name", + resultSetMapping = "person-id-and-name-and-weight" ) } ) @@ -107,6 +124,8 @@ public class ConstructorResultNativeQueryTest extends BaseEntityManagerFunctiona private String name; @Temporal( TemporalType.TIMESTAMP ) private Date birthDate; + @Column( name = "p_weight" ) + private int weight; public Person() { } @@ -121,6 +140,12 @@ public class ConstructorResultNativeQueryTest extends BaseEntityManagerFunctiona this.id = id; this.name = name; } + + public Person(Integer id, String name, String weight) { + this.id = id; + this.name = name; + this.weight = Integer.valueOf(weight); + } } @Override @@ -181,4 +206,27 @@ public class ConstructorResultNativeQueryTest extends BaseEntityManagerFunctiona em.getTransaction().commit(); em.close(); } + + @Test + public void testConstructorResultNativeQuerySpecifyingType() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.persist( new Person( 1, "John", "85" ) ); + em.getTransaction().commit(); + em.close(); + + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + List results = em.createNamedQuery( "person-id-and-name-and-weight" ).getResultList(); + assertEquals( 1, results.size() ); + assertTyping( Person.class, results.get( 0 ) ); + em.getTransaction().commit(); + em.close(); + + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.createQuery( "delete from Person" ).executeUpdate(); + em.getTransaction().commit(); + em.close(); + } }