From 34c86a444f4c0372ea710115d0101c395e7c99e2 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 4 Dec 2025 16:22:59 +0100 Subject: [PATCH 1/2] HHH-19977 Relax scope to allow HR to run a reactive flush A native query might need to run a flush operation before it's executed. Hibernate Reactive needs to runs a reactive flush. By relaxing the scopes of these methods, we can avoid some code duplication. --- .../query/spi/AbstractSelectionQuery.java | 17 ++++++++++++++--- .../query/sql/internal/NativeQueryImpl.java | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java index e6f0fd741e97..d83e37c397c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java @@ -181,16 +181,27 @@ protected void beforeQuery() { session.prepareForQueryExecution( requiresTxn( options.getLockOptions().getLockMode() ) ); prepareForExecution(); + prepareSessionFlushMode( session ); + prepareSessionCacheMode( session ); + } + /* + * Used by Hibernate Reactive + */ + protected void prepareSessionFlushMode(SharedSessionContractImplementor session) { assert sessionFlushMode == null; - assert sessionCacheMode == null; - - final FlushMode effectiveFlushMode = getQueryOptions().getFlushMode(); + final var effectiveFlushMode = getQueryOptions().getFlushMode(); if ( effectiveFlushMode != null && session instanceof SessionImplementor statefulSession ) { sessionFlushMode = statefulSession.getHibernateFlushMode(); statefulSession.setHibernateFlushMode( effectiveFlushMode ); } + } + /* + * Used by Hibernate Reactive + */ + protected void prepareSessionCacheMode(SharedSessionContractImplementor session) { + assert sessionCacheMode == null; final CacheMode effectiveCacheMode = getCacheMode(); if ( effectiveCacheMode != null ) { sessionCacheMode = session.getCacheMode(); diff --git a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java index dd66e3708035..f8c850083b93 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java @@ -568,6 +568,14 @@ public boolean hasCallbackActions() { return callback != null && callback.hasAfterLoadActions(); } + /* + * Used by Hibernate Reactive + */ + @Override + protected void resetCallback() { + callback = null; + } + @Override public QueryParameterBindings getQueryParameterBindings() { return parameterBindings; @@ -736,14 +744,17 @@ protected void prepareForExecution() { getSession().flush(); } // Reset the callback before every execution - callback = null; + resetCallback(); } // Otherwise, the application specified query spaces via the Hibernate // SynchronizeableQuery and so the query will already perform a partial // flush according to the defined query spaces - no need for a full flush. } - private boolean shouldFlush() { + /* + * Used by Hibernate Reactive + */ + protected boolean shouldFlush() { if ( getSession().isTransactionInProgress() ) { final FlushMode flushMode = getQueryOptions().getFlushMode(); return switch ( flushMode == null ? getSession().getHibernateFlushMode() : flushMode ) { From 74d0c2476e946a65efec55fbbf8e48c2a4f3a5d7 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 7 Dec 2025 11:47:08 +0100 Subject: [PATCH 2/2] Update hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java For consistency --- .../java/org/hibernate/query/spi/AbstractSelectionQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java index d83e37c397c6..4edccdf2a934 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/AbstractSelectionQuery.java @@ -202,7 +202,7 @@ protected void prepareSessionFlushMode(SharedSessionContractImplementor session) */ protected void prepareSessionCacheMode(SharedSessionContractImplementor session) { assert sessionCacheMode == null; - final CacheMode effectiveCacheMode = getCacheMode(); + final var effectiveCacheMode = getCacheMode(); if ( effectiveCacheMode != null ) { sessionCacheMode = session.getCacheMode(); session.setCacheMode( effectiveCacheMode );