Skip to content

Commit 6e0def0

Browse files
committed
HHH-15942 introduce ForcedFlushMode for specifying whether a query flushes or not
- replaces FlushModeType in the annotation package - much less confusing when applied to a Query * what do MANUAL and COMMIT mean for a Query? * how is AUTO useful for a Query? - also make Query.getHibernateFlushMode() obey its documented semantics by returning the session flush mode instead of null when unset
1 parent cc46b62 commit 6e0def0

26 files changed

+589
-111
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate;
8+
9+
/**
10+
* Enumerates the possible flush modes for execution of a
11+
* {@link org.hibernate.query.Query}. A "forced" flush mode
12+
* overrides the {@linkplain Session#getHibernateFlushMode()
13+
* flush mode of the session}.
14+
*
15+
* @author Gavin King
16+
*
17+
* @since 6.2
18+
*/
19+
public enum ForcedFlushMode {
20+
/**
21+
* Flush before executing the query.
22+
*/
23+
FORCE_FLUSH,
24+
/**
25+
* Do not flush before executing the query.
26+
*/
27+
FORCE_NO_FLUSH,
28+
/**
29+
* Let the owning {@link Session session} decide whether
30+
* to flush, depending on its {@link FlushMode}.
31+
*/
32+
NO_FORCING
33+
}

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public interface Session extends SharedSessionContract, EntityManager {
148148
void flush();
149149

150150
/**
151-
* Set the current {@link FlushModeType JPA flush mode} for this session.
151+
* Set the current {@linkplain FlushModeType JPA flush mode} for this session.
152152
* <p>
153153
* <em>Flushing</em> is the process of synchronizing the underlying persistent
154154
* store with persistable state held in memory. The current flush mode determines
@@ -162,7 +162,7 @@ public interface Session extends SharedSessionContract, EntityManager {
162162
void setFlushMode(FlushModeType flushMode);
163163

164164
/**
165-
* Set the current {@link FlushMode flush mode} for this session.
165+
* Set the current {@linkplain FlushMode flush mode} for this session.
166166
* <p>
167167
* <em>Flushing</em> is the process of synchronizing the underlying persistent
168168
* store with persistable state held in memory. The current flush mode determines
@@ -180,22 +180,22 @@ public interface Session extends SharedSessionContract, EntityManager {
180180
void setHibernateFlushMode(FlushMode flushMode);
181181

182182
/**
183-
* Get the current {@link FlushModeType JPA flush mode} for this session.
183+
* Get the current {@linkplain FlushModeType JPA flush mode} for this session.
184184
*
185185
* @return the {@link FlushModeType} currently in effect
186186
*/
187187
@Override
188188
FlushModeType getFlushMode();
189189

190190
/**
191-
* Get the current {@link FlushMode flush mode} for this session.
191+
* Get the current {@linkplain FlushMode flush mode} for this session.
192192
*
193193
* @return the {@link FlushMode} currently in effect
194194
*/
195195
FlushMode getHibernateFlushMode();
196196

197197
/**
198-
* Set the current {@link CacheMode cache mode} for this session.
198+
* Set the current {@linkplain CacheMode cache mode} for this session.
199199
* <p>
200200
* The cache mode determines the manner in which this session can interact with
201201
* the second level cache.
@@ -205,7 +205,7 @@ public interface Session extends SharedSessionContract, EntityManager {
205205
void setCacheMode(CacheMode cacheMode);
206206

207207
/**
208-
* Get the current {@link CacheMode cache mode} for this session.
208+
* Get the current {@linkplain CacheMode cache mode} for this session.
209209
*
210210
* @return the current cache mode
211211
*/

hibernate-core/src/main/java/org/hibernate/annotations/FlushModeType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*
1717
* @see NamedQuery
1818
* @see NamedNativeQuery
19+
*
20+
* @deprecated use {@link org.hibernate.ForcedFlushMode}
1921
*/
22+
@Deprecated(since="6")
2023
public enum FlushModeType {
2124
/**
2225
* Corresponds to {@link org.hibernate.FlushMode#ALWAYS}.

hibernate-core/src/main/java/org/hibernate/annotations/NamedNativeQuery.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jakarta.persistence.CacheRetrieveMode;
1414
import jakarta.persistence.CacheStoreMode;
1515
import org.hibernate.CacheMode;
16+
import org.hibernate.ForcedFlushMode;
1617
import org.hibernate.Remove;
1718

1819
import static java.lang.annotation.ElementType.PACKAGE;
@@ -64,12 +65,23 @@
6465
*/
6566
String resultSetMapping() default "";
6667

68+
/**
69+
* Determines whether the session should be flushed before
70+
* executing the query.
71+
*
72+
* @see org.hibernate.query.CommonQueryContract#setForcedFlushMode(ForcedFlushMode)
73+
*/
74+
ForcedFlushMode flush() default ForcedFlushMode.NO_FORCING;
75+
6776
/**
6877
* The flush mode for the query.
6978
*
7079
* @see org.hibernate.query.CommonQueryContract#setFlushMode(jakarta.persistence.FlushModeType)
7180
* @see org.hibernate.jpa.HibernateHints#HINT_FLUSH_MODE
81+
*
82+
* @deprecated use {@link #flush()}
7283
*/
84+
@Deprecated(since = "6")
7385
FlushModeType flushMode() default FlushModeType.PERSISTENCE_CONTEXT;
7486

7587
/**

hibernate-core/src/main/java/org/hibernate/annotations/NamedQuery.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import jakarta.persistence.CacheRetrieveMode;
1414
import jakarta.persistence.CacheStoreMode;
15+
import org.hibernate.ForcedFlushMode;
1516
import org.hibernate.Remove;
1617

1718
import static java.lang.annotation.ElementType.PACKAGE;
@@ -47,12 +48,23 @@
4748
*/
4849
String query();
4950

51+
/**
52+
* Determines whether the session should be flushed before
53+
* executing the query.
54+
*
55+
* @see org.hibernate.query.CommonQueryContract#setForcedFlushMode(ForcedFlushMode)
56+
*/
57+
ForcedFlushMode flush() default ForcedFlushMode.NO_FORCING;
58+
5059
/**
5160
* The flush mode for this query.
5261
*
5362
* @see org.hibernate.query.CommonQueryContract#setFlushMode(jakarta.persistence.FlushModeType)
5463
* @see org.hibernate.jpa.HibernateHints#HINT_FLUSH_MODE
64+
*
65+
* @deprecated use {@link #flush()}
5566
*/
67+
@Deprecated(since = "6")
5668
FlushModeType flushMode() default FlushModeType.PERSISTENCE_CONTEXT;
5769

5870
/**

hibernate-core/src/main/java/org/hibernate/boot/model/internal/QueryBinder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.hibernate.AssertionFailure;
1515
import org.hibernate.CacheMode;
1616
import org.hibernate.FlushMode;
17+
import org.hibernate.ForcedFlushMode;
1718
import org.hibernate.Remove;
1819
import org.hibernate.annotations.CacheModeType;
1920
import org.hibernate.annotations.FlushModeType;
@@ -29,6 +30,7 @@
2930
import org.hibernate.internal.CoreMessageLogger;
3031
import org.hibernate.internal.log.DeprecationLogger;
3132
import org.hibernate.jpa.HibernateHints;
33+
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
3234
import org.hibernate.query.sql.internal.ParameterParser;
3335
import org.hibernate.query.sql.spi.ParameterRecognizer;
3436
import org.hibernate.type.BasicType;
@@ -184,7 +186,7 @@ public static void bindNativeQuery(
184186
.setCacheMode( getCacheMode( namedNativeQuery ) )
185187
.setTimeout( namedNativeQuery.timeout() < 0 ? null : namedNativeQuery.timeout() )
186188
.setFetchSize( namedNativeQuery.fetchSize() < 0 ? null : namedNativeQuery.fetchSize() )
187-
.setFlushMode( getFlushMode( namedNativeQuery.flushMode() ) )
189+
.setFlushMode( getFlushMode( namedNativeQuery.flush(), namedNativeQuery.flushMode() ) )
188190
.setReadOnly( namedNativeQuery.readOnly() )
189191
.setQuerySpaces( setOf( namedNativeQuery.querySpaces() ) )
190192
.setComment(nullIfEmpty(namedNativeQuery.comment()));
@@ -341,7 +343,7 @@ public static void bindQuery(
341343
.setCacheMode( getCacheMode( namedQuery ) )
342344
.setTimeout( namedQuery.timeout() < 0 ? null : namedQuery.timeout() )
343345
.setFetchSize( namedQuery.fetchSize() < 0 ? null : namedQuery.fetchSize() )
344-
.setFlushMode( getFlushMode( namedQuery.flushMode() ) )
346+
.setFlushMode( getFlushMode( namedQuery.flush(), namedQuery.flushMode() ) )
345347
.setReadOnly( namedQuery.readOnly() )
346348
.setComment( nullIfEmpty( namedQuery.comment() ) );
347349

@@ -364,6 +366,12 @@ private static CacheMode getCacheMode(org.hibernate.annotations.NamedNativeQuery
364366
return cacheMode == null || cacheMode == CacheMode.NORMAL ? getCacheMode( namedNativeQuery.cacheMode() ) : cacheMode;
365367
}
366368

369+
private static FlushMode getFlushMode(ForcedFlushMode forcedFlushMode, FlushModeType flushModeType) {
370+
return forcedFlushMode == ForcedFlushMode.NO_FORCING
371+
? getFlushMode( flushModeType )
372+
: FlushModeTypeHelper.getFlushMode( forcedFlushMode );
373+
}
374+
367375
private static FlushMode getFlushMode(FlushModeType flushModeType) {
368376
switch ( flushModeType ) {
369377
case ALWAYS:

hibernate-core/src/main/java/org/hibernate/graph/internal/parse/GraphParser.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import org.hibernate.internal.util.collections.Stack;
1919
import org.hibernate.internal.util.collections.StandardStack;
2020

21-
import org.jboss.logging.Logger;
22-
2321
import org.antlr.v4.runtime.CharStreams;
2422
import org.antlr.v4.runtime.CommonTokenStream;
2523

hibernate-core/src/main/java/org/hibernate/jpa/internal/util/FlushModeTypeHelper.java

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import org.hibernate.AssertionFailure;
1414
import org.hibernate.FlushMode;
15+
import org.hibernate.ForcedFlushMode;
1516
import org.hibernate.MappingException;
1617

1718
import org.jboss.logging.Logger;
@@ -28,33 +29,69 @@ private FlushModeTypeHelper() {
2829
}
2930

3031
public static FlushModeType getFlushModeType(FlushMode flushMode) {
31-
if ( flushMode == FlushMode.ALWAYS ) {
32-
log.debug( "Interpreting Hibernate FlushMode#ALWAYS to JPA FlushModeType#AUTO; may cause problems if relying on FlushMode#ALWAYS-specific behavior" );
33-
return FlushModeType.AUTO;
32+
if ( flushMode == null ) {
33+
return null;
3434
}
35-
else if ( flushMode == FlushMode.MANUAL ) {
36-
log.debug( "Interpreting Hibernate FlushMode#MANUAL to JPA FlushModeType#COMMIT; may cause problems if relying on FlushMode#MANUAL-specific behavior" );
37-
return FlushModeType.COMMIT;
35+
switch ( flushMode ) {
36+
case ALWAYS:
37+
log.debug( "Interpreting Hibernate FlushMode#ALWAYS to JPA FlushModeType#AUTO; may cause problems if relying on FlushMode#ALWAYS-specific behavior" );
38+
return FlushModeType.AUTO;
39+
case MANUAL:
40+
log.debug( "Interpreting Hibernate FlushMode#MANUAL to JPA FlushModeType#COMMIT; may cause problems if relying on FlushMode#MANUAL-specific behavior" );
41+
return FlushModeType.COMMIT;
42+
case COMMIT:
43+
return FlushModeType.COMMIT;
44+
case AUTO:
45+
return FlushModeType.AUTO;
46+
default:
47+
throw new AssertionFailure( "unhandled FlushMode " + flushMode );
3848
}
39-
else if ( flushMode == FlushMode.COMMIT ) {
40-
return FlushModeType.COMMIT;
49+
}
50+
51+
public static ForcedFlushMode getForcedFlushMode(FlushMode flushMode) {
52+
if ( flushMode == null ) {
53+
return ForcedFlushMode.NO_FORCING;
4154
}
42-
else if ( flushMode == FlushMode.AUTO ) {
43-
return FlushModeType.AUTO;
55+
switch ( flushMode ) {
56+
case ALWAYS:
57+
return ForcedFlushMode.FORCE_FLUSH;
58+
case COMMIT:
59+
case MANUAL:
60+
return ForcedFlushMode.FORCE_NO_FLUSH;
61+
case AUTO:
62+
// this is not precisely correctly correct, but good enough
63+
return ForcedFlushMode.NO_FORCING;
64+
default:
65+
throw new AssertionFailure( "unhandled FlushMode " + flushMode );
4466
}
45-
46-
throw new AssertionFailure( "unhandled FlushMode " + flushMode );
4767
}
4868

4969
public static FlushMode getFlushMode(FlushModeType flushModeType) {
50-
if ( flushModeType == FlushModeType.AUTO ) {
51-
return FlushMode.AUTO;
70+
if ( flushModeType == null ) {
71+
return null;
5272
}
53-
else if ( flushModeType == FlushModeType.COMMIT ) {
54-
return FlushMode.COMMIT;
73+
switch ( flushModeType ) {
74+
case AUTO:
75+
return FlushMode.AUTO;
76+
case COMMIT:
77+
return FlushMode.COMMIT;
78+
default:
79+
throw new AssertionFailure( "unhandled FlushModeType " + flushModeType );
5580
}
81+
}
5682

57-
throw new AssertionFailure( "unhandled FlushModeType " + flushModeType );
83+
public static FlushMode getFlushMode(ForcedFlushMode forcedFlushMode) {
84+
if ( forcedFlushMode == null ) {
85+
return null;
86+
}
87+
switch ( forcedFlushMode ) {
88+
case FORCE_FLUSH:
89+
return FlushMode.ALWAYS;
90+
case FORCE_NO_FLUSH:
91+
return FlushMode.MANUAL;
92+
default:
93+
return null;
94+
}
5895
}
5996

6097
public static FlushMode interpretFlushMode(Object value) {

hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ public NamedCallableQueryMemento toMemento(String name) {
784784
isCacheable(),
785785
getCacheRegion(),
786786
getCacheMode(),
787-
getHibernateFlushMode(),
787+
getQueryOptions().getFlushMode(),
788788
isReadOnly(),
789789
getTimeout(),
790790
getFetchSize(),

0 commit comments

Comments
 (0)