diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/LockModeTypeHelper.java b/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/LockModeTypeHelper.java index 97b6f11513..6da1cdadef 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/LockModeTypeHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/internal/util/LockModeTypeHelper.java @@ -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 ) { diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java index ec4dc2f87b..d43b73812a 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/NativeQueryImpl.java @@ -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 extends AbstractProducedQuery 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( diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/NamedQueryTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/NamedQueryTest.java index 309976b4f8..5919627c6b 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/query/NamedQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/query/NamedQueryTest.java @@ -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 = ?"))