HHH-8237 Applying type configured via @ColumnMapping#type() for constructor results

This commit is contained in:
Gunnar Morling 2014-07-01 16:18:39 +02:00
parent 14c00abd6a
commit d84c6b35aa
2 changed files with 56 additions and 11 deletions

View File

@ -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
)
);
}

View File

@ -23,6 +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;
@ -33,22 +39,12 @@ import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.List;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
/**
* @author Steve Ebersole
*/
@ -86,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 )
}
)
}
)
}
)
@ -100,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"
)
}
)
@ -110,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() {
}
@ -124,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
@ -184,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();
}
}