Skip to content

Commit 05dcb8f

Browse files
committed
HHH-8486 - javax.persistence.Tuple#get(String,Class) impl does not validate type
1 parent a3f1c24 commit 05dcb8f

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/spi/AbstractEntityManagerImpl.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,20 @@ public HqlTupleImpl(Object[] tuple) {
481481

482482
@Override
483483
public <X> X get(String alias, Class<X> type) {
484-
return (X) get( alias );
484+
final Object untyped = get( alias );
485+
if ( untyped != null ) {
486+
if ( ! type.isInstance( untyped ) ) {
487+
throw new IllegalArgumentException(
488+
String.format(
489+
"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]",
490+
alias,
491+
untyped,
492+
type.getName()
493+
)
494+
);
495+
}
496+
}
497+
return (X) untyped;
485498
}
486499

487500
@Override
@@ -655,7 +668,20 @@ public Object get(String alias) {
655668
}
656669

657670
public <X> X get(String alias, Class<X> type) {
658-
return ( X ) get( alias );
671+
final Object untyped = get( alias );
672+
if ( untyped != null ) {
673+
if ( ! type.isInstance( untyped ) ) {
674+
throw new IllegalArgumentException(
675+
String.format(
676+
"Requested tuple value [alias=%s, value=%s] cannot be assigned to requested type [%s]",
677+
alias,
678+
untyped,
679+
type.getName()
680+
)
681+
);
682+
}
683+
}
684+
return (X) untyped;
659685
}
660686

661687
public Object get(int i) {

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/tuple/TupleCriteriaTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package org.hibernate.jpa.test.criteria.tuple;
2525

26+
import java.util.Date;
2627
import java.util.List;
2728
import javax.persistence.EntityManager;
2829
import javax.persistence.Tuple;
@@ -38,8 +39,8 @@
3839
import org.hibernate.jpa.test.metamodel.Customer;
3940
import org.hibernate.jpa.test.metamodel.Customer_;
4041

41-
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
4242
import static org.junit.Assert.assertEquals;
43+
import static org.junit.Assert.assertNotNull;
4344
import static org.junit.Assert.assertTrue;
4445
import static org.junit.Assert.fail;
4546

@@ -145,6 +146,52 @@ public void testIllegalArgumentExceptionBuildingTupleWithSameAliases() {
145146
em.close();
146147
}
147148

149+
@Test
150+
public void testVariousTupleAccessMethods() {
151+
EntityManager em = entityManagerFactory().createEntityManager();
152+
em.getTransaction().begin();
153+
Customer c1 = new Customer();
154+
c1.setId( "c1" );
155+
c1.setAge( 18 );
156+
c1.setName( "Bob" );
157+
em.persist( c1 );
158+
em.getTransaction().commit();
159+
em.close();
160+
161+
em = entityManagerFactory().createEntityManager();
162+
em.getTransaction().begin();
163+
164+
final CriteriaBuilder builder = em.getCriteriaBuilder();
165+
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
166+
Root<Customer> customerRoot = criteria.from( Customer.class );
167+
Path<String> namePath = customerRoot.get( Customer_.name );
168+
namePath.alias( "NAME" );
169+
Path<Integer> agePath = customerRoot.get( Customer_.age );
170+
agePath.alias( "AGE" );
171+
criteria.multiselect( namePath, agePath );
172+
173+
List<Tuple> results = em.createQuery( criteria ).getResultList();
174+
Tuple tuple = results.get( 0 );
175+
assertNotNull( tuple );
176+
assertNotNull( tuple.get( "NAME" ) );
177+
assertNotNull( tuple.get( "NAME", String.class ) );
178+
try {
179+
tuple.get( "NAME", Date.class );
180+
fail( "Accessing Customer#name tuple as Date should have thrown exception" );
181+
}
182+
catch (IllegalArgumentException expected) {
183+
}
184+
185+
em.getTransaction().commit();
186+
em.close();
187+
188+
em = entityManagerFactory().createEntityManager();
189+
em.getTransaction().begin();
190+
em.createQuery( "delete Customer" ).executeUpdate();
191+
em.getTransaction().commit();
192+
em.close();
193+
}
194+
148195
@Test
149196
public void testIllegalArgumentExceptionBuildingSelectArrayWithSameAliases() {
150197
EntityManager em = entityManagerFactory().createEntityManager();

0 commit comments

Comments
 (0)