some minor cleanups

This commit is contained in:
Gavin 2023-05-20 14:38:55 +02:00 committed by Gavin King
parent fb28443081
commit 60cec44467
2 changed files with 47 additions and 45 deletions

View File

@ -10,7 +10,6 @@ import org.hibernate.HibernateException;
import org.hibernate.collection.spi.AbstractPersistentCollection; import org.hibernate.collection.spi.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry; import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.EventSource;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;

View File

@ -25,7 +25,6 @@ import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.spi.CollectionEntry; import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status; import org.hibernate.engine.spi.Status;
import org.hibernate.engine.spi.TypedValue; import org.hibernate.engine.spi.TypedValue;
@ -36,12 +35,13 @@ import org.hibernate.internal.util.MarkerObject;
import org.hibernate.internal.util.collections.IdentitySet; import org.hibernate.internal.util.collections.IdentitySet;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.resource.transaction.spi.TransactionStatus; import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.hibernate.type.BasicType; import org.hibernate.type.BasicType;
import org.hibernate.type.CompositeType; import org.hibernate.type.CompositeType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
/** /**
* Base class implementing {@link PersistentCollection} * Base class implementing {@link PersistentCollection}
* *
@ -147,9 +147,8 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
else { else {
return withTemporarySessionIfNeeded( return withTemporarySessionIfNeeded(
() -> { () -> {
final CollectionEntry entry = session.getPersistenceContextInternal() final CollectionEntry entry =
.getCollectionEntry(this); session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry != null ) { if ( entry != null ) {
final CollectionPersister persister = entry.getLoadedPersister(); final CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) { if ( persister.isExtraLazy() ) {
@ -178,7 +177,7 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
if ( cachedSize>=0 ) { if ( cachedSize>=0 ) {
return cachedSize; return cachedSize;
} }
CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry(this); final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry == null ) { if ( entry == null ) {
throwLazyInitializationExceptionIfNotConnected(); throwLazyInitializationExceptionIfNotConnected();
throwLazyInitializationException("collection not associated with session"); throwLazyInitializationException("collection not associated with session");
@ -254,8 +253,8 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
session.beginTransaction(); session.beginTransaction();
} }
final CollectionPersister collectionDescriptor = session.getSessionFactory() final CollectionPersister collectionDescriptor =
.getRuntimeMetamodels() session.getSessionFactory()
.getMappingMetamodel() .getMappingMetamodel()
.getCollectionDescriptor( getRole() ); .getCollectionDescriptor( getRole() );
session.getPersistenceContextInternal().addUninitializedDetachedCollection( collectionDescriptor, this ); session.getPersistenceContextInternal().addUninitializedDetachedCollection( collectionDescriptor, this );
@ -289,8 +288,8 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
final PersistenceContext persistenceContext = session.getPersistenceContext(); final PersistenceContext persistenceContext = session.getPersistenceContext();
if ( !session.isTransactionInProgress() if ( !session.isTransactionInProgress()
&& ( !persistenceContext.hasLoadContext() && ( !persistenceContext.hasLoadContext()
|| ( persistenceContext.hasLoadContext() || persistenceContext.hasLoadContext()
&& persistenceContext.getLoadContexts().isLoadingFinished() ) ) ) { && persistenceContext.getLoadContexts().isLoadingFinished() ) ) {
session.getJdbcCoordinator().afterTransaction(); session.getJdbcCoordinator().afterTransaction();
} }
} }
@ -302,8 +301,8 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
throwLazyInitializationException( "SessionFactory UUID not known to create temporary Session for loading" ); throwLazyInitializationException( "SessionFactory UUID not known to create temporary Session for loading" );
} }
final SessionFactoryImplementor sf = SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid ); final SharedSessionContractImplementor session =
final SharedSessionContractImplementor session = sf.openSession(); SessionFactoryRegistry.INSTANCE.getSessionFactory( sessionFactoryUuid ).openSession();
session.getPersistenceContextInternal().setDefaultReadOnly( true ); session.getPersistenceContextInternal().setDefaultReadOnly( true );
session.setHibernateFlushMode( FlushMode.MANUAL ); session.setHibernateFlushMode( FlushMode.MANUAL );
return session; return session;
@ -355,7 +354,7 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
@Override @Override
public boolean elementExists(Object element) { public boolean elementExists(Object element) {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this ); final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry == null ) { if ( entry == null ) {
throwLazyInitializationExceptionIfNotConnected(); throwLazyInitializationExceptionIfNotConnected();
throwLazyInitializationException("collection not associated with session"); throwLazyInitializationException("collection not associated with session");
@ -379,7 +378,9 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
@Override @Override
public Object doWork() { public Object doWork() {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this ); final CollectionEntry entry =
session.getPersistenceContextInternal()
.getCollectionEntry( AbstractPersistentCollection.this );
final CollectionPersister persister = entry.getLoadedPersister(); final CollectionPersister persister = entry.getLoadedPersister();
isExtraLazy = persister.isExtraLazy(); isExtraLazy = persister.isExtraLazy();
if ( isExtraLazy ) { if ( isExtraLazy ) {
@ -407,7 +408,7 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
@Override @Override
public Object elementByIndex(Object index) { public Object elementByIndex(Object index) {
final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( AbstractPersistentCollection.this ); final CollectionEntry entry = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( entry == null ) { if ( entry == null ) {
throwLazyInitializationExceptionIfNotConnected(); throwLazyInitializationExceptionIfNotConnected();
throwLazyInitializationException("collection not associated with session"); throwLazyInitializationException("collection not associated with session");
@ -522,7 +523,7 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
} }
/** /**
* Replace entity instances with copy in {@code copyCache}/. * Replace entity instances with copy in {@code copyCache}.
* *
* @param copyCache - mapping from entity in the process of being * @param copyCache - mapping from entity in the process of being
* merged to managed copy. * merged to managed copy.
@ -608,17 +609,15 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
* @throws LazyInitializationException if we cannot initialize * @throws LazyInitializationException if we cannot initialize
*/ */
protected final void initialize(final boolean writing) { protected final void initialize(final boolean writing) {
if ( initialized ) { if ( !initialized ) {
return;
}
withTemporarySessionIfNeeded( withTemporarySessionIfNeeded(
() -> { () -> {
session.initializeCollection( AbstractPersistentCollection.this, writing ); session.initializeCollection( this, writing );
return null; return null;
} }
); );
} }
}
void throwLazyInitializationExceptionIfNotConnected() { void throwLazyInitializationExceptionIfNotConnected() {
if ( !isConnectedToSession() ) { if ( !isConnectedToSession() ) {
@ -662,7 +661,7 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
if ( currentSession == this.session ) { if ( currentSession == this.session ) {
if ( !isTempSession ) { if ( !isTempSession ) {
if ( hasQueuedOperations() ) { if ( hasQueuedOperations() ) {
final String collectionInfoString = MessageHelper.collectionInfoString( getRole(), getKey() ); final String collectionInfoString = collectionInfoString( getRole(), getKey() );
try { try {
final TransactionStatus transactionStatus = final TransactionStatus transactionStatus =
session.getTransactionCoordinator().getTransactionDriverControl().getStatus(); session.getTransactionCoordinator().getTransactionDriverControl().getStatus();
@ -688,8 +687,10 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
LOG.queuedOperationWhenDetachFromSession( collectionInfoString ); LOG.queuedOperationWhenDetachFromSession( collectionInfoString );
} }
} }
if ( allowLoadOutsideTransaction && !initialized && session.getLoadQueryInfluencers().hasEnabledFilters() ) { if ( allowLoadOutsideTransaction
final String collectionInfoString = MessageHelper.collectionInfoString( getRole(), getKey() ); && !initialized
&& session.getLoadQueryInfluencers().hasEnabledFilters() ) {
final String collectionInfoString = collectionInfoString( getRole(), getKey() );
LOG.enabledFiltersWhenDetachFromSession( collectionInfoString ); LOG.enabledFiltersWhenDetachFromSession( collectionInfoString );
} }
this.session = null; this.session = null;
@ -706,7 +707,9 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
protected void prepareForPossibleLoadingOutsideTransaction() { protected void prepareForPossibleLoadingOutsideTransaction() {
if ( session != null ) { if ( session != null ) {
allowLoadOutsideTransaction = session.getFactory().getSessionFactoryOptions().isInitializeLazyStateOutsideTransactionsEnabled(); allowLoadOutsideTransaction =
session.getFactory().getSessionFactoryOptions()
.isInitializeLazyStateOutsideTransactionsEnabled();
if ( allowLoadOutsideTransaction && sessionFactoryUuid == null ) { if ( allowLoadOutsideTransaction && sessionFactoryUuid == null ) {
sessionFactoryUuid = session.getFactory().getUuid(); sessionFactoryUuid = session.getFactory().getUuid();
@ -731,7 +734,7 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
} }
} }
if ( hasQueuedOperations() ) { if ( hasQueuedOperations() ) {
LOG.queuedOperationWhenAttachToSession( MessageHelper.collectionInfoString( getRole(), getKey() ) ); LOG.queuedOperationWhenAttachToSession( collectionInfoString( getRole(), getKey() ) );
} }
this.session = session; this.session = session;
return true; return true;
@ -751,13 +754,13 @@ public abstract class AbstractPersistentCollection<E> implements Serializable, P
final StringBuilder sb = new StringBuilder( "Collection : " ); final StringBuilder sb = new StringBuilder( "Collection : " );
if ( roleCurrent != null ) { if ( roleCurrent != null ) {
sb.append( MessageHelper.collectionInfoString( roleCurrent, keyCurrent ) ); sb.append( collectionInfoString( roleCurrent, keyCurrent ) );
} }
else { else {
final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this ); final CollectionEntry ce = session.getPersistenceContextInternal().getCollectionEntry( this );
if ( ce != null ) { if ( ce != null ) {
sb.append( sb.append(
MessageHelper.collectionInfoString( collectionInfoString(
ce.getLoadedPersister(), ce.getLoadedPersister(),
this, this,
ce.getLoadedKey(), ce.getLoadedKey(),