Skip to content

Commit 436e4bf

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 436e4bf

25 files changed

+575
-105
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hibernate;
2+
3+
/**
4+
* Enumerates the possible flush modes for execution of a
5+
* {@link org.hibernate.query.Query}.
6+
*
7+
* @author Gavin King
8+
*
9+
* @since 6.2
10+
*/
11+
public enum ForcedFlushMode {
12+
/**
13+
* Flush before executing the query.
14+
*/
15+
FORCE_FLUSH,
16+
/**
17+
* Do not flush before executing the query.
18+
*/
19+
FORCE_NO_FLUSH,
20+
/**
21+
* Let the owning {@link Session session} decide whether
22+
* to flush, depending on its {@link FlushMode}.
23+
*/
24+
NO_FORCING
25+
}

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(),

hibernate-core/src/main/java/org/hibernate/query/CommonQueryContract.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414

1515
import org.hibernate.FlushMode;
16+
import org.hibernate.ForcedFlushMode;
1617
import org.hibernate.Session;
1718

1819
import jakarta.persistence.FlushModeType;
@@ -30,36 +31,66 @@
3031
*/
3132
public interface CommonQueryContract {
3233

34+
/**
35+
* The {@link ForcedFlushMode} in effect for this query.
36+
* <p>
37+
* By default, this is {@link ForcedFlushMode#NO_FORCING}, and the
38+
* {@link FlushMode} of the owning {@link Session} determines whether
39+
* it is flushed.
40+
*
41+
* @see Session#getHibernateFlushMode()
42+
*/
43+
ForcedFlushMode getForcedFlushMode();
44+
45+
/**
46+
* Set the {@link ForcedFlushMode} in to use for this query.
47+
*
48+
* @see Session#getHibernateFlushMode()
49+
*/
50+
CommonQueryContract setForcedFlushMode(ForcedFlushMode forcedFlushMode);
51+
3352
/**
3453
* The JPA {@link FlushModeType} in effect for this query. By default, the
3554
* query inherits the {@link FlushMode} of the {@link Session} from which
3655
* it originates.
3756
*
38-
* @see #getHibernateFlushMode
39-
* @see Session#getHibernateFlushMode
57+
* @see #getForcedFlushMode()
58+
* @see #getHibernateFlushMode()
59+
* @see Session#getHibernateFlushMode()
60+
*
61+
* @deprecated use {@link #getForcedFlushMode()}
4062
*/
63+
@Deprecated(since = "6")
4164
FlushModeType getFlushMode();
4265

4366
/**
4467
* Set the {@link FlushMode} in to use for this query.
68+
* <p>
69+
* Setting this to {@code null} ultimately indicates to use the
70+
* {@link FlushMode} of the Session. Use {@link #setHibernateFlushMode}
71+
* passing {@link FlushMode#MANUAL} instead to indicate that no automatic
72+
* flushing should occur.
4573
*
46-
* @implNote Setting to {@code null} ultimately indicates to use the
47-
* FlushMode of the Session. Use {@link #setHibernateFlushMode} passing
48-
* {@link FlushMode#MANUAL} instead to indicate that no automatic flushing
49-
* should occur
50-
*
74+
* @see #getForcedFlushMode()
5175
* @see #getHibernateFlushMode()
5276
* @see Session#getHibernateFlushMode()
77+
*
78+
* @deprecated use {@link #setForcedFlushMode(ForcedFlushMode)}
5379
*/
80+
@Deprecated(since = "6")
5481
CommonQueryContract setFlushMode(FlushModeType flushMode);
5582

5683
/**
57-
* The {@link FlushMode} in effect for this query. By default, the query
84+
* The {@link FlushMode} in effect for this query. By default, the query
5885
* inherits the {@code FlushMode} of the {@link Session} from which it
5986
* originates.
6087
*
61-
* @see Session#getHibernateFlushMode
88+
* @see #getForcedFlushMode()
89+
* @see Session#getHibernateFlushMode()
90+
*
91+
* @deprecated use {@link #getForcedFlushMode()}
6292
*/
93+
@Deprecated(since = "6")
6394
FlushMode getHibernateFlushMode();
6495

6596
/**
@@ -69,9 +100,13 @@ public interface CommonQueryContract {
69100
* {@link FlushMode} of the Session. Use {@link FlushMode#MANUAL}
70101
* instead to indicate that no automatic flushing should occur.
71102
*
103+
* @see #getForcedFlushMode()
72104
* @see #getHibernateFlushMode()
73105
* @see Session#getHibernateFlushMode()
106+
*
107+
* @deprecated use {@link #setForcedFlushMode(ForcedFlushMode)}
74108
*/
109+
@Deprecated(since = "6")
75110
CommonQueryContract setHibernateFlushMode(FlushMode flushMode);
76111

77112
/**

hibernate-core/src/main/java/org/hibernate/query/MutationQuery.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414

1515
import org.hibernate.FlushMode;
16+
import org.hibernate.ForcedFlushMode;
1617
import org.hibernate.Incubating;
1718

1819
import jakarta.persistence.Parameter;
@@ -195,4 +196,7 @@ public interface MutationQuery extends CommonQueryContract {
195196

196197
@Override
197198
MutationQuery setHibernateFlushMode(FlushMode flushMode);
199+
200+
@Override
201+
MutationQuery setForcedFlushMode(ForcedFlushMode forcedFlushMode);
198202
}

0 commit comments

Comments
 (0)