Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/ForcedFlushMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate;

/**
* Enumerates the possible flush modes for execution of a
* {@link org.hibernate.query.Query}. A "forced" flush mode
* overrides the {@linkplain Session#getHibernateFlushMode()
* flush mode of the session}.
*
* @author Gavin King
*
* @since 6.2
*/
public enum ForcedFlushMode {
/**
* Flush before executing the query.
*/
FORCE_FLUSH,
/**
* Do not flush before executing the query.
*/
FORCE_NO_FLUSH,
/**
* Let the owning {@link Session session} decide whether
* to flush, depending on its {@link FlushMode}.
*/
NO_FORCING
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everyone, please take a look at this new thing!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is solely about queries, I'd be more inclined to name this QueryFlushMode or move it into o.h.query. For QueryFlushMode, I'd go with:

enum QueryFlushMode {
    FLUSH,
    SKIP,
    /**
     * Defer to {@link Session#getHibernateFlushMode}
     */
    AUTO
}

I get why you named it as you did. I just find it confusing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. QueryFlushMode is fine by me, it was also my first thought.
  2. You're right, I agree it belongs in o.h.query.
  3. FLUSH is fine.
  4. SKIP I don't find extremely clear though I could live with it.
  5. I don't like AUTO here because it doesn't have the same semantics as FlushMode.AUTO.

12 changes: 6 additions & 6 deletions hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public interface Session extends SharedSessionContract, EntityManager {
void flush();

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

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

/**
* Get the current {@link FlushModeType JPA flush mode} for this session.
* Get the current {@linkplain FlushModeType JPA flush mode} for this session.
*
* @return the {@link FlushModeType} currently in effect
*/
@Override
FlushModeType getFlushMode();

/**
* Get the current {@link FlushMode flush mode} for this session.
* Get the current {@linkplain FlushMode flush mode} for this session.
*
* @return the {@link FlushMode} currently in effect
*/
FlushMode getHibernateFlushMode();

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

/**
* Get the current {@link CacheMode cache mode} for this session.
* Get the current {@linkplain CacheMode cache mode} for this session.
*
* @return the current cache mode
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*
* @see NamedQuery
* @see NamedNativeQuery
*
* @deprecated use {@link org.hibernate.ForcedFlushMode}
*/
@Deprecated(since="6")
public enum FlushModeType {
/**
* Corresponds to {@link org.hibernate.FlushMode#ALWAYS}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import org.hibernate.CacheMode;
import org.hibernate.ForcedFlushMode;
import org.hibernate.Remove;

import static java.lang.annotation.ElementType.PACKAGE;
Expand Down Expand Up @@ -64,12 +65,23 @@
*/
String resultSetMapping() default "";

/**
* Determines whether the session should be flushed before
* executing the query.
*
* @see org.hibernate.query.CommonQueryContract#setForcedFlushMode(ForcedFlushMode)
*/
ForcedFlushMode flush() default ForcedFlushMode.NO_FORCING;

/**
* The flush mode for the query.
*
* @see org.hibernate.query.CommonQueryContract#setFlushMode(jakarta.persistence.FlushModeType)
* @see org.hibernate.jpa.HibernateHints#HINT_FLUSH_MODE
*
* @deprecated use {@link #flush()}
*/
@Deprecated(since = "6")
FlushModeType flushMode() default FlushModeType.PERSISTENCE_CONTEXT;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import org.hibernate.ForcedFlushMode;
import org.hibernate.Remove;

import static java.lang.annotation.ElementType.PACKAGE;
Expand Down Expand Up @@ -47,12 +48,23 @@
*/
String query();

/**
* Determines whether the session should be flushed before
* executing the query.
*
* @see org.hibernate.query.CommonQueryContract#setForcedFlushMode(ForcedFlushMode)
*/
ForcedFlushMode flush() default ForcedFlushMode.NO_FORCING;

/**
* The flush mode for this query.
*
* @see org.hibernate.query.CommonQueryContract#setFlushMode(jakarta.persistence.FlushModeType)
* @see org.hibernate.jpa.HibernateHints#HINT_FLUSH_MODE
*
* @deprecated use {@link #flush()}
*/
@Deprecated(since = "6")
FlushModeType flushMode() default FlushModeType.PERSISTENCE_CONTEXT;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.AssertionFailure;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.ForcedFlushMode;
import org.hibernate.Remove;
import org.hibernate.annotations.CacheModeType;
import org.hibernate.annotations.FlushModeType;
Expand All @@ -29,6 +30,7 @@
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.log.DeprecationLogger;
import org.hibernate.jpa.HibernateHints;
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
import org.hibernate.query.sql.internal.ParameterParser;
import org.hibernate.query.sql.spi.ParameterRecognizer;
import org.hibernate.type.BasicType;
Expand Down Expand Up @@ -184,7 +186,7 @@ public static void bindNativeQuery(
.setCacheMode( getCacheMode( namedNativeQuery ) )
.setTimeout( namedNativeQuery.timeout() < 0 ? null : namedNativeQuery.timeout() )
.setFetchSize( namedNativeQuery.fetchSize() < 0 ? null : namedNativeQuery.fetchSize() )
.setFlushMode( getFlushMode( namedNativeQuery.flushMode() ) )
.setFlushMode( getFlushMode( namedNativeQuery.flush(), namedNativeQuery.flushMode() ) )

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation

Invoking [NamedNativeQuery.flushMode](1) should be avoided because it has been deprecated.
.setReadOnly( namedNativeQuery.readOnly() )
.setQuerySpaces( setOf( namedNativeQuery.querySpaces() ) )
.setComment(nullIfEmpty(namedNativeQuery.comment()));
Expand Down Expand Up @@ -341,7 +343,7 @@ public static void bindQuery(
.setCacheMode( getCacheMode( namedQuery ) )
.setTimeout( namedQuery.timeout() < 0 ? null : namedQuery.timeout() )
.setFetchSize( namedQuery.fetchSize() < 0 ? null : namedQuery.fetchSize() )
.setFlushMode( getFlushMode( namedQuery.flushMode() ) )
.setFlushMode( getFlushMode( namedQuery.flush(), namedQuery.flushMode() ) )

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation

Invoking [NamedQuery.flushMode](1) should be avoided because it has been deprecated.
.setReadOnly( namedQuery.readOnly() )
.setComment( nullIfEmpty( namedQuery.comment() ) );

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

private static FlushMode getFlushMode(ForcedFlushMode forcedFlushMode, FlushModeType flushModeType) {
return forcedFlushMode == ForcedFlushMode.NO_FORCING
? getFlushMode( flushModeType )
: FlushModeTypeHelper.getFlushMode( forcedFlushMode );
}

private static FlushMode getFlushMode(FlushModeType flushModeType) {
switch ( flushModeType ) {
case ALWAYS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.hibernate.internal.util.collections.Stack;
import org.hibernate.internal.util.collections.StandardStack;

import org.jboss.logging.Logger;

import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.hibernate.AssertionFailure;
import org.hibernate.FlushMode;
import org.hibernate.ForcedFlushMode;
import org.hibernate.MappingException;

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

public static FlushModeType getFlushModeType(FlushMode flushMode) {
if ( flushMode == FlushMode.ALWAYS ) {
log.debug( "Interpreting Hibernate FlushMode#ALWAYS to JPA FlushModeType#AUTO; may cause problems if relying on FlushMode#ALWAYS-specific behavior" );
return FlushModeType.AUTO;
if ( flushMode == null ) {
return null;
}
else if ( flushMode == FlushMode.MANUAL ) {
log.debug( "Interpreting Hibernate FlushMode#MANUAL to JPA FlushModeType#COMMIT; may cause problems if relying on FlushMode#MANUAL-specific behavior" );
return FlushModeType.COMMIT;
switch ( flushMode ) {
case ALWAYS:
log.debug( "Interpreting Hibernate FlushMode#ALWAYS to JPA FlushModeType#AUTO; may cause problems if relying on FlushMode#ALWAYS-specific behavior" );
return FlushModeType.AUTO;
case MANUAL:
log.debug( "Interpreting Hibernate FlushMode#MANUAL to JPA FlushModeType#COMMIT; may cause problems if relying on FlushMode#MANUAL-specific behavior" );
return FlushModeType.COMMIT;
case COMMIT:
return FlushModeType.COMMIT;
case AUTO:
return FlushModeType.AUTO;
default:
throw new AssertionFailure( "unhandled FlushMode " + flushMode );
}
else if ( flushMode == FlushMode.COMMIT ) {
return FlushModeType.COMMIT;
}

public static ForcedFlushMode getForcedFlushMode(FlushMode flushMode) {
if ( flushMode == null ) {
return ForcedFlushMode.NO_FORCING;
}
else if ( flushMode == FlushMode.AUTO ) {
return FlushModeType.AUTO;
switch ( flushMode ) {
case ALWAYS:
return ForcedFlushMode.FORCE_FLUSH;
case COMMIT:
case MANUAL:
return ForcedFlushMode.FORCE_NO_FLUSH;
case AUTO:
// this is not precisely correctly correct, but good enough
return ForcedFlushMode.NO_FORCING;
default:
throw new AssertionFailure( "unhandled FlushMode " + flushMode );
}

throw new AssertionFailure( "unhandled FlushMode " + flushMode );
}

public static FlushMode getFlushMode(FlushModeType flushModeType) {
if ( flushModeType == FlushModeType.AUTO ) {
return FlushMode.AUTO;
if ( flushModeType == null ) {
return null;
}
else if ( flushModeType == FlushModeType.COMMIT ) {
return FlushMode.COMMIT;
switch ( flushModeType ) {
case AUTO:
return FlushMode.AUTO;
case COMMIT:
return FlushMode.COMMIT;
default:
throw new AssertionFailure( "unhandled FlushModeType " + flushModeType );
}
}

throw new AssertionFailure( "unhandled FlushModeType " + flushModeType );
public static FlushMode getFlushMode(ForcedFlushMode forcedFlushMode) {
if ( forcedFlushMode == null ) {
return null;
}
switch ( forcedFlushMode ) {
case FORCE_FLUSH:
return FlushMode.ALWAYS;
case FORCE_NO_FLUSH:
return FlushMode.MANUAL;
default:
return null;
}
}

public static FlushMode interpretFlushMode(Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public NamedCallableQueryMemento toMemento(String name) {
isCacheable(),
getCacheRegion(),
getCacheMode(),
getHibernateFlushMode(),
getQueryOptions().getFlushMode(),
isReadOnly(),
getTimeout(),
getFetchSize(),
Expand Down
Loading