Skip to content

Commit 818f52c

Browse files
committed
partially address a todo in SharedSessionContractImplementor
- remove deprecated getFlushMode() - remove setHibernateFlushMode() - deprecate silly methods which are much worse than safe casts
1 parent 34fb646 commit 818f52c

File tree

18 files changed

+97
-137
lines changed

18 files changed

+97
-137
lines changed

hibernate-core/src/main/java/org/hibernate/action/internal/EntityIncrementVersionProcess.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ public EntityIncrementVersionProcess(Object object) {
3737
public void doBeforeTransactionCompletion(SessionImplementor session) {
3838
final EntityEntry entry = session.getPersistenceContext().getEntry( object );
3939
// Don't increment version for an entity that is not in the PersistenceContext;
40-
if ( entry == null ) {
41-
return;
40+
if ( entry != null ) {
41+
OptimisticLockHelper.forceVersionIncrement( object, entry, session.asEventSource() );
4242
}
43-
44-
OptimisticLockHelper.forceVersionIncrement( object, entry, session.asEventSource() );
4543
}
4644
}

hibernate-core/src/main/java/org/hibernate/bytecode/enhance/spi/interceptor/EnhancementHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.hibernate.FlushMode;
1111
import org.hibernate.LazyInitializationException;
1212
import org.hibernate.engine.spi.SessionFactoryImplementor;
13+
import org.hibernate.engine.spi.SessionImplementor;
1314
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1415
import org.hibernate.internal.SessionFactoryRegistry;
1516
import org.hibernate.mapping.Collection;
@@ -281,7 +282,7 @@ private static SharedSessionContractImplementor openTemporarySessionForLoading(
281282
}
282283

283284
final SessionFactoryImplementor sf = SessionFactoryRegistry.INSTANCE.getSessionFactory( interceptor.getSessionFactoryUuid() );
284-
final SharedSessionContractImplementor session = sf.openSession();
285+
final SessionImplementor session = sf.openSession();
285286
session.getPersistenceContextInternal().setDefaultReadOnly( true );
286287
session.setHibernateFlushMode( FlushMode.MANUAL );
287288
return session;

hibernate-core/src/main/java/org/hibernate/collection/spi/AbstractPersistentCollection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.hibernate.engine.spi.CollectionEntry;
2525
import org.hibernate.engine.spi.EntityEntry;
2626
import org.hibernate.engine.spi.PersistenceContext;
27+
import org.hibernate.engine.spi.SessionImplementor;
2728
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2829
import org.hibernate.engine.spi.Status;
2930
import org.hibernate.engine.spi.TypedValue;
@@ -316,7 +317,7 @@ private SharedSessionContractImplementor openTemporarySessionForLoading() {
316317
throwLazyInitializationException( "SessionFactory UUID not known to create temporary Session for loading" );
317318
}
318319

319-
final SharedSessionContractImplementor session =
320+
final SessionImplementor session =
320321
SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid ).openSession();
321322
session.getPersistenceContextInternal().setDefaultReadOnly( true );
322323
session.setHibernateFlushMode( FlushMode.MANUAL );

hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public void postUpdate(Object entity, Object[] updatedState, Object nextVersion)
288288

289289
final SharedSessionContractImplementor session = getPersistenceContext().getSession();
290290
session.getFactory().getCustomEntityDirtinessStrategy()
291-
.resetDirty( entity, persister, session.asSessionImplementor() );
291+
.resetDirty( entity, persister, (SessionImplementor) session );
292292
}
293293

294294
private static void clearDirtyAttributes(final SelfDirtinessTracker entity) {
@@ -371,7 +371,7 @@ private boolean isNonDirtyViaCustomStrategy(Object entity) {
371371
}
372372
}
373373

374-
final SessionImplementor session = getPersistenceContext().getSession().asSessionImplementor();
374+
final SessionImplementor session = (SessionImplementor) getPersistenceContext().getSession();
375375
final CustomEntityDirtinessStrategy customEntityDirtinessStrategy =
376376
session.getFactory().getCustomEntityDirtinessStrategy();
377377
return customEntityDirtinessStrategy.canDirtyCheck( entity, getPersister(), session )

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ default SessionImplementor getSession() {
8383
*/
8484
ActionQueue getActionQueue();
8585

86+
@Override
8687
Object instantiate(EntityPersister persister, Object id) throws HibernateException;
8788

8889
/**
@@ -94,16 +95,6 @@ default SessionImplementor getSession() {
9495
*/
9596
void forceFlush(EntityKey e) throws HibernateException;
9697

97-
@Override
98-
default SessionImplementor asSessionImplementor() {
99-
return this;
100-
}
101-
102-
@Override
103-
default boolean isSessionImplementor() {
104-
return true;
105-
}
106-
10798
@Override
10899
default <C> void runWithConnection(ConnectionConsumer<C> action) {
109100
doWork( connection -> {

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionContractImplementor.java

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.util.Set;
88
import java.util.UUID;
9-
import jakarta.persistence.FlushModeType;
109
import jakarta.persistence.TransactionRequiredException;
1110
import org.checkerframework.checker.nullness.qual.Nullable;
1211

@@ -73,14 +72,6 @@ public interface SharedSessionContractImplementor
7372
extends SharedSessionContract, JdbcSessionOwner, Options, LobCreationContext, WrapperOptions,
7473
QueryProducerImplementor, JavaType.CoercionContext {
7574

76-
// todo : this is the shared contract between Session and StatelessSession,
77-
// but it defines methods that StatelessSession does not implement
78-
// To me it seems like it is better to properly isolate those methods
79-
// into just the Session hierarchy. They include (at least):
80-
// 1) get/set CacheMode
81-
// 2) get/set FlushMode
82-
// 3) get/set (default) read-only
83-
8475
/**
8576
* Obtain the {@linkplain SessionFactoryImplementor factory} which created this session.
8677
*/
@@ -383,36 +374,6 @@ default String bestGuessEntityName(Object object, EntityEntry entry) {
383374

384375
void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored);
385376

386-
/**
387-
* Get the current {@link FlushModeType} for this session.
388-
* <p>
389-
* For users of the Hibernate native APIs, we've had to rename this method
390-
* as defined by Hibernate historically because the JPA contract defines a method of the same
391-
* name, but returning the JPA {@link FlushModeType} rather than Hibernate's {@link FlushMode}.
392-
* For the former behavior, use {@link #getHibernateFlushMode()} instead.
393-
*
394-
* @return The {@link FlushModeType} in effect for this Session.
395-
*
396-
* @deprecated there's no good reason to expose this here
397-
*/
398-
@Deprecated(since = "6")
399-
FlushModeType getFlushMode();
400-
401-
/**
402-
* Set the current {@link FlushMode} for this session.
403-
* <p>
404-
* The flush mode determines the points at which the session is flushed.
405-
* <em>Flushing</em> is the process of synchronizing the underlying persistent
406-
* store with persistable state held in memory.
407-
* <p>
408-
* For a logically "read-only" session, it's reasonable to set the session
409-
* flush mode to {@link FlushMode#MANUAL} at the start of the session
410-
* (in order skip some work and gain some extra performance).
411-
*
412-
* @param flushMode the new flush mode
413-
*/
414-
void setHibernateFlushMode(FlushMode flushMode);
415-
416377
/**
417378
* Get the current {@link FlushMode} for this session.
418379
*
@@ -533,15 +494,22 @@ default Integer getConfiguredJdbcBatchSize() {
533494
*
534495
* @return true if flush is required, false otherwise.
535496
*/
536-
boolean autoFlushIfRequired(Set<String> querySpaces) throws HibernateException;
537-
538-
default boolean autoFlushIfRequired(Set<String> querySpaces, boolean skipPreFlush)
539-
throws HibernateException {
540-
return autoFlushIfRequired( querySpaces );
497+
default boolean autoFlushIfRequired(Set<String> querySpaces) {
498+
return autoFlushIfRequired( querySpaces, false );
541499
}
542500

543-
default void autoPreFlush(){
544-
}
501+
/**
502+
* detect in-memory changes, determine if the changes are to tables
503+
* named in the query and, if so, complete execution the flush
504+
*
505+
* @param querySpaces the tables named in the query.
506+
* @param skipPreFlush see {@link org.hibernate.event.spi.AutoFlushEvent#isSkipPreFlush}
507+
*
508+
* @return true if flush is required, false otherwise.
509+
*/
510+
boolean autoFlushIfRequired(Set<String> querySpaces, boolean skipPreFlush);
511+
512+
void autoPreFlush();
545513

546514
/**
547515
* Check if there is a Hibernate or JTA transaction in progress and,
@@ -557,32 +525,44 @@ default void autoPreFlush(){
557525
* Cast this object to {@link SessionImplementor}, if possible.
558526
*
559527
* @throws ClassCastException if the cast is not possible
528+
*
529+
* @deprecated No longer useful, since Java made downcasting safer
560530
*/
531+
@Deprecated(since = "7.0", forRemoval = true)
561532
default SessionImplementor asSessionImplementor() {
562533
throw new ClassCastException( "session is not a SessionImplementor" );
563534
}
564535

565536
/**
566537
* Does this object implement {@link SessionImplementor}?
538+
*
539+
* @deprecated No longer useful, since Java made downcasting safer
567540
*/
541+
@Deprecated(since = "7.0", forRemoval = true)
568542
default boolean isSessionImplementor() {
569-
return false;
543+
return this instanceof SessionImplementor;
570544
}
571545

572546
/**
573547
* Cast this object to {@link StatelessSession}, if possible.
574548
*
575549
* @throws ClassCastException if the cast is not possible
550+
*
551+
* @deprecated No longer useful, since Java made downcasting safer
576552
*/
553+
@Deprecated(since = "7.0", forRemoval = true)
577554
default StatelessSession asStatelessSession() {
578-
throw new ClassCastException( "session is not a StatelessSession" );
555+
return (StatelessSession) this;
579556
}
580557

581558
/**
582559
* Does this object implement {@link StatelessSession}?
560+
*
561+
* @deprecated No longer useful, since Java made downcasting safer
583562
*/
563+
@Deprecated(since = "7.0", forRemoval = true)
584564
default boolean isStatelessSession() {
585-
return false;
565+
return this instanceof StatelessSession;
586566
}
587567

588568
/**

hibernate-core/src/main/java/org/hibernate/engine/spi/SharedSessionDelegatorBaseImpl.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.engine.spi;
66

77
import jakarta.persistence.EntityGraph;
8-
import jakarta.persistence.FlushModeType;
98
import jakarta.persistence.TypedQueryReference;
109
import jakarta.persistence.criteria.CriteriaDelete;
1110
import jakarta.persistence.criteria.CriteriaQuery;
@@ -514,16 +513,6 @@ public void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored)
514513
delegate.setNativeJdbcParametersIgnored( nativeJdbcParametersIgnored );
515514
}
516515

517-
@Override
518-
public FlushModeType getFlushMode() {
519-
return delegate.getFlushMode();
520-
}
521-
522-
@Override
523-
public void setHibernateFlushMode(FlushMode flushMode) {
524-
delegate.setHibernateFlushMode( flushMode );
525-
}
526-
527516
@Override
528517
public FlushMode getHibernateFlushMode() {
529518
return delegate.getHibernateFlushMode();

hibernate-core/src/main/java/org/hibernate/event/spi/AutoFlushEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Set<String> getQuerySpaces() {
3232
return querySpaces;
3333
}
3434

35-
public void setQuerySpaces(Set querySpaces) {
35+
public void setQuerySpaces(Set<String> querySpaces) {
3636
this.querySpaces = querySpaces;
3737
}
3838

hibernate-core/src/main/java/org/hibernate/id/IdentifierGeneratorHelper.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import java.util.Properties;
1616

1717
import org.hibernate.Internal;
18+
import org.hibernate.Session;
19+
import org.hibernate.StatelessSession;
1820
import org.hibernate.TransientObjectException;
1921
import org.hibernate.boot.registry.selector.spi.StrategySelector;
2022
import org.hibernate.engine.config.spi.ConfigurationService;
23+
import org.hibernate.engine.spi.SessionImplementor;
2124
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2225
import org.hibernate.id.enhanced.ImplicitDatabaseObjectNamingStrategy;
2326
import org.hibernate.id.enhanced.StandardNamingStrategy;
@@ -135,8 +138,8 @@ public static Object getForeignId(
135138
final EntityPersister entityDescriptor =
136139
sessionImplementor.getFactory().getMappingMetamodel()
137140
.getEntityDescriptor( entityName );
138-
if ( sessionImplementor.isSessionImplementor()
139-
&& sessionImplementor.asSessionImplementor().contains( entityName, object ) ) {
141+
if ( sessionImplementor instanceof SessionImplementor statefulSession
142+
&& statefulSession.contains( entityName, object ) ) {
140143
//abort the save (the object is already saved by a circular cascade)
141144
return SHORT_CIRCUIT_INDICATOR;
142145
//throw new IdentifierGenerationException("save associated object first, or disable cascade for inverse association");
@@ -166,12 +169,12 @@ private static Object identifier(
166169
return getEntityIdentifierIfNotUnsaved( associatedEntityName, associatedEntity, sessionImplementor );
167170
}
168171
catch (TransientObjectException toe) {
169-
if ( sessionImplementor.isSessionImplementor() ) {
170-
sessionImplementor.asSessionImplementor().persist( associatedEntityName, associatedEntity );
172+
if ( sessionImplementor instanceof Session statefulSession ) {
173+
statefulSession.persist( associatedEntityName, associatedEntity );
171174
return sessionImplementor.getContextEntityIdentifier( associatedEntity );
172175
}
173-
else if ( sessionImplementor.isStatelessSession() ) {
174-
return sessionImplementor.asStatelessSession().insert( associatedEntityName, associatedEntity );
176+
else if ( sessionImplementor instanceof StatelessSession statelessSession ) {
177+
return statelessSession.insert( associatedEntityName, associatedEntity );
175178
}
176179
else {
177180
throw new IdentifierGenerationException("sessionImplementor is neither Session nor StatelessSession");

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.hibernate.CacheMode;
2222
import org.hibernate.EntityNameResolver;
2323
import org.hibernate.Filter;
24-
import org.hibernate.FlushMode;
2524
import org.hibernate.HibernateException;
2625
import org.hibernate.Interceptor;
2726
import org.hibernate.LockMode;
@@ -104,7 +103,6 @@
104103
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl;
105104
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
106105

107-
import jakarta.persistence.FlushModeType;
108106
import jakarta.persistence.TransactionRequiredException;
109107
import jakarta.persistence.TypedQueryReference;
110108
import jakarta.persistence.criteria.CriteriaDelete;
@@ -116,7 +114,6 @@
116114

117115
import static java.lang.Boolean.TRUE;
118116
import static org.hibernate.internal.util.StringHelper.isEmpty;
119-
import static org.hibernate.jpa.internal.util.FlushModeTypeHelper.getFlushModeType;
120117
import static org.hibernate.query.sqm.internal.SqmUtil.verifyIsSelectStatement;
121118

122119
/**
@@ -162,7 +159,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
162159
private final TimeZone jdbcTimeZone;
163160

164161
// mutable state
165-
private FlushMode flushMode;
166162
private CacheMode cacheMode;
167163
private Integer jdbcBatchSize;
168164

@@ -187,7 +183,6 @@ public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreation
187183
this.jdbcServices = factory.getJdbcServices();
188184

189185
cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this );
190-
flushMode = options.getInitialSessionFlushMode();
191186
tenantIdentifier = getTenantId( factoryOptions, options );
192187
interceptor = interpret( options.getInterceptor() );
193188
jdbcTimeZone = options.getJdbcTimeZone();
@@ -764,22 +759,6 @@ public JdbcServices getJdbcServices() {
764759
return jdbcServices;
765760
}
766761

767-
@Override
768-
public FlushModeType getFlushMode() {
769-
checkOpen();
770-
return getFlushModeType( flushMode );
771-
}
772-
773-
@Override
774-
public void setHibernateFlushMode(FlushMode flushMode) {
775-
this.flushMode = flushMode;
776-
}
777-
778-
@Override
779-
public FlushMode getHibernateFlushMode() {
780-
return flushMode;
781-
}
782-
783762
@Override
784763
public CacheMode getCacheMode() {
785764
return cacheMode;

0 commit comments

Comments
 (0)