some minor cleanups to CollectionType

This commit is contained in:
Gavin King 2022-09-24 12:19:14 +02:00
parent bb996705b7
commit b7f93a04cf
3 changed files with 32 additions and 48 deletions

View File

@ -58,7 +58,7 @@ public class CustomCollectionTypeSemantics<CE, E> implements CollectionSemantics
@Override @Override
public Iterator<E> getElementIterator(CE rawCollection) { public Iterator<E> getElementIterator(CE rawCollection) {
//noinspection unchecked //noinspection unchecked
return (Iterator<E>) collectionType.getElementsIterator( rawCollection, null ); return (Iterator<E>) collectionType.getElementsIterator( rawCollection );
} }
@Override @Override

View File

@ -476,7 +476,7 @@ public class CascadingActions {
EventSource session, EventSource session,
CollectionType collectionType, CollectionType collectionType,
Object collection) { Object collection) {
return collectionType.getElementsIterator( collection, session ); return collectionType.getElementsIterator( collection );
} }
/** /**
@ -489,7 +489,7 @@ public class CascadingActions {
Object collection) { Object collection) {
if ( collectionIsInitialized( collection ) ) { if ( collectionIsInitialized( collection ) ) {
// handles arrays and newly instantiated collections // handles arrays and newly instantiated collections
return collectionType.getElementsIterator( collection, session ); return collectionType.getElementsIterator( collection );
} }
else { else {
// does not handle arrays (thats ok, cos they can't be lazy) // does not handle arrays (thats ok, cos they can't be lazy)

View File

@ -58,7 +58,7 @@ public abstract class CollectionType extends AbstractType implements Association
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, CollectionType.class.getName()); private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, CollectionType.class.getName());
private static final Object NOT_NULL_COLLECTION = new MarkerObject( "NOT NULL COLLECTION" ); // private static final Object NOT_NULL_COLLECTION = new MarkerObject( "NOT NULL COLLECTION" );
public static final Object UNFETCHED_COLLECTION = new MarkerObject( "UNFETCHED COLLECTION" ); public static final Object UNFETCHED_COLLECTION = new MarkerObject( "UNFETCHED COLLECTION" );
private final String role; private final String role;
@ -87,7 +87,7 @@ public abstract class CollectionType extends AbstractType implements Association
public boolean contains(Object collection, Object childObject, SharedSessionContractImplementor session) { public boolean contains(Object collection, Object childObject, SharedSessionContractImplementor session) {
// we do not have to worry about queued additions to uninitialized // we do not have to worry about queued additions to uninitialized
// collections, since they can only occur for inverse collections! // collections, since they can only occur for inverse collections!
Iterator<?> elems = getElementsIterator( collection, session ); Iterator<?> elems = getElementsIterator( collection );
while ( elems.hasNext() ) { while ( elems.hasNext() ) {
Object element = elems.next(); Object element = elems.next();
// worrying about proxies is perhaps a little bit of overkill here... // worrying about proxies is perhaps a little bit of overkill here...
@ -112,12 +112,13 @@ public abstract class CollectionType extends AbstractType implements Association
@Override @Override
public final boolean isEqual(Object x, Object y) { public final boolean isEqual(Object x, Object y) {
return x == y return x == y
|| ( x instanceof PersistentCollection && isEqual( (PersistentCollection<?>) x, y ) ) || x instanceof PersistentCollection && isEqual( (PersistentCollection<?>) x, y )
|| ( y instanceof PersistentCollection && isEqual( (PersistentCollection<?>) y, x ) ); || y instanceof PersistentCollection && isEqual( (PersistentCollection<?>) y, x );
} }
private boolean isEqual(PersistentCollection<?> x, Object y) { private boolean isEqual(PersistentCollection<?> x, Object y) {
return x.wasInitialized() && ( x.isWrapper( y ) || x.isDirectlyProvidedCollection( y ) ); return x.wasInitialized()
&& ( x.isWrapper( y ) || x.isDirectlyProvidedCollection( y ) );
} }
@Override @Override
@ -196,13 +197,11 @@ public abstract class CollectionType extends AbstractType implements Association
Iterator<?> itr = getElementsIterator( value ); Iterator<?> itr = getElementsIterator( value );
while ( itr.hasNext() ) { while ( itr.hasNext() ) {
Object element = itr.next(); Object element = itr.next();
if ( element == LazyPropertyInitializer.UNFETCHED_PROPERTY String string =
|| !Hibernate.isInitialized( element ) ) { element == LazyPropertyInitializer.UNFETCHED_PROPERTY || !Hibernate.isInitialized(element)
list.add( "<uninitialized>" ); ? "<uninitialized>"
} : elemType.toLoggableString( element, factory );
else { list.add( string );
list.add( elemType.toLoggableString( element, factory ) );
}
} }
return list.toString(); return list.toString();
} }
@ -223,18 +222,21 @@ public abstract class CollectionType extends AbstractType implements Association
* @param collection The collection to be iterated * @param collection The collection to be iterated
* @param session The session from which the request is originating. * @param session The session from which the request is originating.
* @return The iterator. * @return The iterator.
*
* @deprecated use {@link #getElementsIterator(Object)}
*/ */
@Deprecated
public Iterator<?> getElementsIterator(Object collection, SharedSessionContractImplementor session) { public Iterator<?> getElementsIterator(Object collection, SharedSessionContractImplementor session) {
return getElementsIterator( collection ); return getElementsIterator( collection );
} }
/** /**
* Get an iterator over the element set of the collection in POJO mode * Get an iterator over the element set of the collection, which may not yet be wrapped
* *
* @param collection The collection to be iterated * @param collection The collection to be iterated
* @return The iterator. * @return The element iterator
*/ */
protected Iterator<?> getElementsIterator(Object collection) { public Iterator<?> getElementsIterator(Object collection) {
return ( (Collection<?>) collection ).iterator(); return ( (Collection<?>) collection ).iterator();
} }
@ -252,15 +254,8 @@ public abstract class CollectionType extends AbstractType implements Association
//what if the collection was null, and then later had elements added? seems unsafe //what if the collection was null, and then later had elements added? seems unsafe
//session.getPersistenceContext().getCollectionEntry( (PersistentCollection) value ).getKey(); //session.getPersistenceContext().getCollectionEntry( (PersistentCollection) value ).getKey();
final Object key = getKeyOfOwner(owner, session); final Object key = getKeyOfOwner( owner, session );
if ( key == null ) { return key == null ? null : getPersister( session ).getKeyType().disassemble( key, session, owner );
return null;
}
else {
return getPersister(session)
.getKeyType()
.disassemble( key, session, owner );
}
} }
@Override @Override
@ -268,7 +263,7 @@ public abstract class CollectionType extends AbstractType implements Association
throws HibernateException { throws HibernateException {
//we must use the "remembered" uk value, since it is //we must use the "remembered" uk value, since it is
//not available from the EntityEntry during assembly //not available from the EntityEntry during assembly
if (cached==null) { if ( cached == null ) {
return null; return null;
} }
else { else {
@ -288,12 +283,7 @@ public abstract class CollectionType extends AbstractType implements Association
*/ */
private CollectionPersister getPersister(SharedSessionContractImplementor session) { private CollectionPersister getPersister(SharedSessionContractImplementor session) {
CollectionPersister p = this.persister; CollectionPersister p = this.persister;
if ( p != null ) { return p != null ? p : getPersister( session.getFactory() );
return p;
}
else {
return getPersister( session.getFactory() );
}
} }
private CollectionPersister getPersister(SessionFactoryImplementor factory) { private CollectionPersister getPersister(SessionFactoryImplementor factory) {
@ -386,20 +376,14 @@ public abstract class CollectionType extends AbstractType implements Association
// later in the mapping document) - now, we could try and use e.getStatus() // later in the mapping document) - now, we could try and use e.getStatus()
// to decide to semiResolve(), trouble is that initializeEntity() reuses // to decide to semiResolve(), trouble is that initializeEntity() reuses
// the same array for resolved and hydrated values // the same array for resolved and hydrated values
Object id; Object id = entityEntry.getLoadedState() != null
if ( entityEntry.getLoadedState() != null ) { ? entityEntry.getLoadedValue( foreignKeyPropertyName )
id = entityEntry.getLoadedValue( foreignKeyPropertyName ); : entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName );
}
else {
id = entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName );
}
// NOTE VERY HACKISH WORKAROUND!! // NOTE VERY HACKISH WORKAROUND!!
// TODO: Fix this so it will work for non-POJO entity mode // TODO: Fix this so it will work for non-POJO entity mode
Type keyType = getPersister( session ).getKeyType(); Type keyType = getPersister( session ).getKeyType();
Class<?> returnedClass = keyType.getReturnedClass(); if ( !keyType.getReturnedClass().isInstance( id ) ) {
if ( !returnedClass.isInstance( id ) ) {
// todo (6.0) : // todo (6.0) :
throw new NotYetImplementedFor6Exception( "Re-work support for semi-resolve" ); throw new NotYetImplementedFor6Exception( "Re-work support for semi-resolve" );
// id = keyType.semiResolve( // id = keyType.semiResolve(
@ -433,8 +417,8 @@ public abstract class CollectionType extends AbstractType implements Association
EntityPersister ownerPersister = persister.getOwnerEntityPersister(); EntityPersister ownerPersister = persister.getOwnerEntityPersister();
// TODO: Fix this so it will work for non-POJO entity mode // TODO: Fix this so it will work for non-POJO entity mode
Class<?> ownerMappedClass = ownerPersister.getMappedClass(); Class<?> ownerMappedClass = ownerPersister.getMappedClass();
if ( ownerMappedClass.isAssignableFrom( keyType.getReturnedClass() ) && if ( ownerMappedClass.isAssignableFrom( keyType.getReturnedClass() )
keyType.getReturnedClass().isInstance( key ) ) { && keyType.getReturnedClass().isInstance( key ) ) {
// the key is the owning entity itself, so get the ID from the key // the key is the owning entity itself, so get the ID from the key
ownerId = ownerPersister.getIdentifier( key, session ); ownerId = ownerPersister.getIdentifier( key, session );
} }
@ -514,7 +498,7 @@ public abstract class CollectionType extends AbstractType implements Association
Object original, Object original,
Object target, Object target,
Object owner, Object owner,
Map copyCache, Map<Object, Object> copyCache,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
Collection result = (Collection) target; Collection result = (Collection) target;
result.clear(); result.clear();
@ -551,7 +535,7 @@ public abstract class CollectionType extends AbstractType implements Association
PersistentCollection<?> result, PersistentCollection<?> result,
Type elemType, Type elemType,
Object owner, Object owner,
Map copyCache, Map<Object, Object> copyCache,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
Serializable originalSnapshot = original.getStoredSnapshot(); Serializable originalSnapshot = original.getStoredSnapshot();
Serializable resultSnapshot = result.getStoredSnapshot(); Serializable resultSnapshot = result.getStoredSnapshot();