HHH-8486 - javax.persistence.Tuple#get(String,Class) impl does not validate type
This commit is contained in:
parent
a3f1c247bc
commit
05dcb8f5c1
|
@ -481,7 +481,20 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
|
|||
|
||||
@Override
|
||||
public <X> X get(String alias, Class<X> 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> X get(String alias, Class<X> 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) {
|
||||
|
|
|
@ -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<Tuple> criteria = builder.createTupleQuery();
|
||||
Root<Customer> customerRoot = criteria.from( Customer.class );
|
||||
Path<String> namePath = customerRoot.get( Customer_.name );
|
||||
namePath.alias( "NAME" );
|
||||
Path<Integer> agePath = customerRoot.get( Customer_.age );
|
||||
agePath.alias( "AGE" );
|
||||
criteria.multiselect( namePath, agePath );
|
||||
|
||||
List<Tuple> 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();
|
||||
|
|
Loading…
Reference in New Issue