HHH-14816 Can not set lock mode with QueryHint due to type case problem

This commit is contained in:
Dariush Moshiri 2021-10-12 13:10:36 +02:00 committed by Sanne Grinovero
parent 8cf51a601b
commit 8e0f0b4123
3 changed files with 20 additions and 2 deletions

View File

@ -40,12 +40,12 @@ public final class LockModeTypeHelper {
}
else if ( String.class.isInstance( value ) ) {
// first try LockMode name
LockMode lockMode = LockMode.valueOf( (String) value );
LockMode lockMode = LockMode.fromExternalForm( (String) value );
if ( lockMode == null ) {
try {
lockMode = getLockMode( LockModeType.valueOf( (String) value ) );
}
catch ( Exception ignore ) {
catch (Exception ignore) {
}
}
if ( lockMode != null ) {

View File

@ -47,6 +47,7 @@ import org.hibernate.graph.GraphSemantic;
import org.hibernate.graph.RootGraph;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.ParameterMetadata;
import org.hibernate.query.Query;
@ -613,6 +614,9 @@ public class NativeQueryImpl<T> extends AbstractProducedQuery<T> implements Nati
else if ( LockModeType.class.isInstance( value ) ) {
applyLockModeTypeHint( (LockModeType) value );
}
else if ( String.class.isInstance( value ) ) {
applyHibernateLockModeHint( LockModeTypeHelper.interpretLockMode( value ) );
}
else {
throw new IllegalArgumentException(
String.format(

View File

@ -16,7 +16,9 @@ import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.jpa.QueryHints;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.query.NativeQuery;
@ -192,6 +194,18 @@ public class NamedQueryTest extends BaseEntityManagerFunctionalTestCase {
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14816")
public void testQueryHintLockMode() {
doInJPA( this::entityManagerFactory, entityManager -> {
Query query = entityManager.createNamedQuery( "NamedNativeQuery" );
query.setHint( QueryHints.HINT_NATIVE_LOCKMODE, "none" );
query.setParameter( 1, GAME_TITLES[0] );
assertEquals( LockMode.NONE, query.getHints().get( QueryHints.HINT_NATIVE_LOCKMODE ) );
}
);
}
@Entity(name = "Game")
@NamedQueries(@NamedQuery(name = "NamedQuery", query = "select g from Game g where title = ?1"))
@NamedNativeQueries(@NamedNativeQuery(name = "NamedNativeQuery", query = "select * from Game g where title = ?"))