HHH-12099 - Query#getLockMode ought to throw exception for non-SELECT

This commit is contained in:
Steve Ebersole 2017-11-16 13:46:25 -06:00
parent 409596235c
commit d1f54b976e
2 changed files with 39 additions and 0 deletions

View File

@ -1223,6 +1223,9 @@ public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
@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() );
}

View File

@ -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 )
);
}
}