diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java index 66c1a8b91e..a2e3ab6334 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java @@ -1223,6 +1223,9 @@ public abstract class AbstractProducedQuery implements QueryImplementor { @Override public LockModeType getLockMode() { + if ( !isSelect() ) { + throw new IllegalStateException( "Illegal attempt to get lock mode on a non-SELECT query" ); + } return LockModeTypeHelper.getLockModeType( lockOptions.getLockMode() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/NonSelectQueryLockMode.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/NonSelectQueryLockMode.java new file mode 100644 index 0000000000..b68708aeaa --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/NonSelectQueryLockMode.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.test.jpa.compliance; + +import javax.persistence.LockModeType; + +import org.hibernate.test.jpa.AbstractJPATest; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction; + +/** + * @author Steve Ebersole + */ +public class NonSelectQueryLockMode extends AbstractJPATest { + + @Test( expected = IllegalStateException.class ) + public void testNonSelectQueryGetLockMode() { + inTransaction( + sessionFactory(), + session -> session.createQuery( "delete Item" ).getLockMode() + ); + } + + @Test( expected = IllegalStateException.class ) + public void testNonSelectQuerySetLockMode() { + inTransaction( + sessionFactory(), + session -> session.createQuery( "delete Item" ).setLockMode( LockModeType.PESSIMISTIC_WRITE ) + ); + } +}