From 05dcb8f5c1112106a3ca394ade70a9e6c9747d34 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 10 Sep 2013 18:46:23 -0500 Subject: [PATCH] HHH-8486 - javax.persistence.Tuple#get(String,Class) impl does not validate type --- .../jpa/spi/AbstractEntityManagerImpl.java | 30 +++++++++++- .../criteria/tuple/TupleCriteriaTest.java | 49 ++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java index aed21f44b8..3230ae6587 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java @@ -481,7 +481,20 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage @Override public X get(String alias, Class type) { - return (X) get( alias ); + final Object untyped = get( alias ); + if ( untyped != null ) { + if ( ! type.isInstance( untyped ) ) { + throw new IllegalArgumentException( + String.format( + "Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]", + alias, + untyped, + type.getName() + ) + ); + } + } + return (X) untyped; } @Override @@ -655,7 +668,20 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage } public X get(String alias, Class type) { - return ( X ) get( alias ); + final Object untyped = get( alias ); + if ( untyped != null ) { + if ( ! type.isInstance( untyped ) ) { + throw new IllegalArgumentException( + String.format( + "Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]", + alias, + untyped, + type.getName() + ) + ); + } + } + return (X) untyped; } public Object get(int i) { diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/tuple/TupleCriteriaTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/tuple/TupleCriteriaTest.java index 354069d415..40096c6313 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/tuple/TupleCriteriaTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/tuple/TupleCriteriaTest.java @@ -23,6 +23,7 @@ */ package org.hibernate.jpa.test.criteria.tuple; +import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Tuple; @@ -38,8 +39,8 @@ import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest; import org.hibernate.jpa.test.metamodel.Customer; import org.hibernate.jpa.test.metamodel.Customer_; -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -145,6 +146,52 @@ public class TupleCriteriaTest extends AbstractMetamodelSpecificTest { em.close(); } + @Test + public void testVariousTupleAccessMethods() { + EntityManager em = entityManagerFactory().createEntityManager(); + em.getTransaction().begin(); + Customer c1 = new Customer(); + c1.setId( "c1" ); + c1.setAge( 18 ); + c1.setName( "Bob" ); + em.persist( c1 ); + em.getTransaction().commit(); + em.close(); + + em = entityManagerFactory().createEntityManager(); + em.getTransaction().begin(); + + final CriteriaBuilder builder = em.getCriteriaBuilder(); + CriteriaQuery criteria = builder.createTupleQuery(); + Root customerRoot = criteria.from( Customer.class ); + Path namePath = customerRoot.get( Customer_.name ); + namePath.alias( "NAME" ); + Path agePath = customerRoot.get( Customer_.age ); + agePath.alias( "AGE" ); + criteria.multiselect( namePath, agePath ); + + List results = em.createQuery( criteria ).getResultList(); + Tuple tuple = results.get( 0 ); + assertNotNull( tuple ); + assertNotNull( tuple.get( "NAME" ) ); + assertNotNull( tuple.get( "NAME", String.class ) ); + try { + tuple.get( "NAME", Date.class ); + fail( "Accessing Customer#name tuple as Date should have thrown exception" ); + } + catch (IllegalArgumentException expected) { + } + + em.getTransaction().commit(); + em.close(); + + em = entityManagerFactory().createEntityManager(); + em.getTransaction().begin(); + em.createQuery( "delete Customer" ).executeUpdate(); + em.getTransaction().commit(); + em.close(); + } + @Test public void testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases() { EntityManager em = entityManagerFactory().createEntityManager();