Skip to content

Commit

Permalink
HHH-8486 - javax.persistence.Tuple#get(String,Class) impl does not va…
Browse files Browse the repository at this point in the history
…lidate type
  • Loading branch information
sebersole committed Sep 10, 2013
1 parent a3f1c24 commit 05dcb8f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
Expand Up @@ -481,7 +481,20 @@ public HqlTupleImpl(Object[] tuple) {

@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
Expand Down Expand Up @@ -655,7 +668,20 @@ public Object get(String alias) {
}

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) {
Expand Down
Expand Up @@ -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;
Expand All @@ -38,8 +39,8 @@
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;

Expand Down Expand Up @@ -145,6 +146,52 @@ public void testIllegalArgumentExceptionBuildingTupleWithSameAliases() {
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();
Expand Down

0 comments on commit 05dcb8f

Please sign in to comment.