From 8f7b8e10d06a4d116c5371aa2d8da0cf716125ef Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 20 Mar 2012 19:24:46 -0500 Subject: [PATCH] HHH-7192 - NullPointerException in QueryTranslatorImpl on erroneously typed delete query --- .../ejb/AbstractEntityManagerImpl.java | 19 +++++++++------ .../hibernate/ejb/test/query/QueryTest.java | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java index 820c08a0af..53155a51c1 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java @@ -295,7 +295,18 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage // do the translation 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 ) ) { // no validation needed } @@ -304,12 +315,6 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage hqlQuery.setResultTransformer( tupleTransformer ); } else { - final SessionImplementor session = unwrap( SessionImplementor.class ); - final HQLQueryPlan queryPlan = session.getFactory().getQueryPlanCache().getHQLQueryPlan( - jpaqlString, - false, - session.getLoadQueryInfluencers().getEnabledFilters() - ); final Class dynamicInstantiationClass = queryPlan.getDynamicInstantiationResultType(); if ( dynamicInstantiationClass != null ) { if ( ! resultClass.isAssignableFrom( dynamicInstantiationClass ) ) { diff --git a/hibernate-entitymanager/src/matrix/java/org/hibernate/ejb/test/query/QueryTest.java b/hibernate-entitymanager/src/matrix/java/org/hibernate/ejb/test/query/QueryTest.java index 5b429bf8c9..02f10ef0a0 100644 --- a/hibernate-entitymanager/src/matrix/java/org/hibernate/ejb/test/query/QueryTest.java +++ b/hibernate-entitymanager/src/matrix/java/org/hibernate/ejb/test/query/QueryTest.java @@ -39,6 +39,8 @@ import org.hibernate.ejb.test.Wallet; import org.junit.Test; +import org.hibernate.testing.TestForIssue; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -46,8 +48,30 @@ import static org.junit.Assert.fail; /** * @author Emmanuel Bernard + * @author Steve Ebersole */ 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 public void testPagedQuery() throws Exception { EntityManager em = getOrCreateEntityManager();