From c5893e7919c0a1c4c2e00514166af3721369225c Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 13 Apr 2023 15:29:58 +0200 Subject: [PATCH] HHH-16461 @Version + session.refresh(entity, LockMode.PESSIMISTIC_WRITE) leads to StaleObjectStateException --- .../sqm/internal/SqmJdbcExecutionContextAdapter.java | 4 ++++ .../java/org/hibernate/sql/exec/spi/ExecutionContext.java | 8 ++++++++ .../results/graph/entity/AbstractEntityInitializer.java | 2 +- .../results/internal/RowProcessingStateStandardImpl.java | 5 +++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmJdbcExecutionContextAdapter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmJdbcExecutionContextAdapter.java index b2635b9835..76853d8638 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmJdbcExecutionContextAdapter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmJdbcExecutionContextAdapter.java @@ -75,4 +75,8 @@ public class SqmJdbcExecutionContextAdapter extends BaseExecutionContext { return true; } + @Override + public boolean upgradeLocks() { + return true; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java index 34e05bfd77..1f10a1b76b 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/ExecutionContext.java @@ -90,4 +90,12 @@ public interface ExecutionContext { return false; } + /** + * Does this query return objects that might be already cached + * by the session, whose lock mode may need upgrading + */ + default boolean upgradeLocks(){ + return false; + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java index 4e75bf3b3c..4b124d1946 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java @@ -571,7 +571,7 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces } private void upgradeLockMode(RowProcessingState rowProcessingState) { - if ( lockMode != LockMode.NONE ) { + if ( lockMode != LockMode.NONE && rowProcessingState.upgradeLocks() ) { final EntityEntry entry = rowProcessingState.getSession().getPersistenceContextInternal() .getEntry( entityInstance ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java index 846228ac8c..790f6df236 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java @@ -201,4 +201,9 @@ public class RowProcessingStateStandardImpl extends BaseExecutionContext impleme public boolean hasCollectionInitializers() { return this.initializers.hasCollectionInitializers(); } + + @Override + public boolean upgradeLocks() { + return executionContext.upgradeLocks(); + } }