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();