diff --git a/hibernate-core/src/main/java/org/hibernate/LockOptions.java b/hibernate-core/src/main/java/org/hibernate/LockOptions.java index 2979c34abc..76370383f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/LockOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/LockOptions.java @@ -48,13 +48,13 @@ public class LockOptions implements Serializable { * Represents {@link LockMode#NONE}, to which timeout and scope are * not applicable. */ - public static final LockOptions NONE = new LockOptions(LockMode.NONE); + public static final LockOptions NONE = new ImmutableLockOptions(LockMode.NONE); /** * Represents {@link LockMode#READ}, to which timeout and scope are * not applicable. */ - public static final LockOptions READ = new LockOptions(LockMode.READ); + public static final LockOptions READ = new ImmutableLockOptions(LockMode.READ); /** * Represents {@link LockMode#PESSIMISTIC_WRITE} with @@ -62,7 +62,7 @@ public class LockOptions implements Serializable { * {@linkplain PessimisticLockScope#NORMAL no extension of the * lock to owned collections}. */ - public static final LockOptions UPGRADE = new LockOptions(LockMode.PESSIMISTIC_WRITE); + public static final LockOptions UPGRADE = new ImmutableLockOptions(LockMode.PESSIMISTIC_WRITE); /** * Indicates that the database should not wait at all to acquire @@ -478,4 +478,40 @@ public class LockOptions implements Serializable { public int hashCode() { return Objects.hash( lockMode, timeout, aliasSpecificLockModes, followOnLocking, scope ); } + + private static class ImmutableLockOptions extends LockOptions { + private ImmutableLockOptions(LockMode lockMode) { + super(lockMode); + } + + @Override + public LockOptions setLockMode(LockMode lockMode) { + throw new UnsupportedOperationException(); + } + + @Override + public LockOptions setLockScope(PessimisticLockScope scope) { + throw new UnsupportedOperationException(); + } + + @Override + public LockOptions setFollowOnLocking(Boolean followOnLocking) { + return super.setFollowOnLocking(followOnLocking); + } + + @Override + public LockOptions setTimeOut(int timeout) { + throw new UnsupportedOperationException(); + } + + @Override + public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) { + throw new UnsupportedOperationException(); + } + + @Override + public LockOptions setScope(boolean scope) { + throw new UnsupportedOperationException(); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryOptionsAdapter.java b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryOptionsAdapter.java index 1a1de60254..21f9f655df 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryOptionsAdapter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryOptionsAdapter.java @@ -37,7 +37,7 @@ public abstract class QueryOptionsAdapter implements QueryOptions { @Override public LockOptions getLockOptions() { - return LockOptions.NONE; + return new LockOptions(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java index 93d3d9dc6b..7d927716ae 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java @@ -421,9 +421,9 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { final RowReader rowReader = ResultsHelper.createRowReader( executionContext, - // If follow on locking is used, we must omit the lock options here, + // If follow-on locking is used, we must omit the lock options here, // because these lock options are only for Initializers. - // If we wouldn't omit this, the follow on lock requests would be no-ops, + // If we wouldn't omit this, the follow-on lock requests would be no-ops, // because the EntityEntrys would already have the desired lock mode deferredResultSetAccess.usesFollowOnLocking() ? LockOptions.NONE