HHH-7192 - NullPointerException in QueryTranslatorImpl on erroneously typed delete query

This commit is contained in:
Steve Ebersole 2012-03-20 19:24:46 -05:00
parent 4f64b56a88
commit 8f7b8e10d0
2 changed files with 36 additions and 7 deletions

View File

@ -295,7 +295,18 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
// do the translation // do the translation
org.hibernate.Query hqlQuery = getSession().createQuery( jpaqlString ); org.hibernate.Query hqlQuery = getSession().createQuery( jpaqlString );
// do some validation checking // make sure the query is a select -> HHH-7192
final SessionImplementor session = unwrap( SessionImplementor.class );
final HQLQueryPlan queryPlan = session.getFactory().getQueryPlanCache().getHQLQueryPlan(
jpaqlString,
false,
session.getLoadQueryInfluencers().getEnabledFilters()
);
if ( queryPlan.getTranslators()[0].isManipulationStatement() ) {
throw new IllegalArgumentException( "Update/delete queries cannot be typed" );
}
// do some return type validation checking
if ( Object[].class.equals( resultClass ) ) { if ( Object[].class.equals( resultClass ) ) {
// no validation needed // no validation needed
} }
@ -304,12 +315,6 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
hqlQuery.setResultTransformer( tupleTransformer ); hqlQuery.setResultTransformer( tupleTransformer );
} }
else { else {
final SessionImplementor session = unwrap( SessionImplementor.class );
final HQLQueryPlan queryPlan = session.getFactory().getQueryPlanCache().getHQLQueryPlan(
jpaqlString,
false,
session.getLoadQueryInfluencers().getEnabledFilters()
);
final Class dynamicInstantiationClass = queryPlan.getDynamicInstantiationResultType(); final Class dynamicInstantiationClass = queryPlan.getDynamicInstantiationResultType();
if ( dynamicInstantiationClass != null ) { if ( dynamicInstantiationClass != null ) {
if ( ! resultClass.isAssignableFrom( dynamicInstantiationClass ) ) { if ( ! resultClass.isAssignableFrom( dynamicInstantiationClass ) ) {

View File

@ -39,6 +39,8 @@ import org.hibernate.ejb.test.Wallet;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -46,8 +48,30 @@ import static org.junit.Assert.fail;
/** /**
* @author Emmanuel Bernard * @author Emmanuel Bernard
* @author Steve Ebersole
*/ */
public class QueryTest extends BaseEntityManagerFunctionalTestCase { public class QueryTest extends BaseEntityManagerFunctionalTestCase {
@Test
@TestForIssue( jiraKey = "HHH-7192" )
public void testTypedManipulationQueryError() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.createQuery( "delete Item", Item.class );
fail();
}
catch (IllegalArgumentException expected) {
}
try {
em.createQuery( "update Item i set i.name = 'someName'", Item.class );
fail();
}
catch (IllegalArgumentException expected) {
}
}
@Test @Test
public void testPagedQuery() throws Exception { public void testPagedQuery() throws Exception {
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();