FileDocCategorySizeDatePackage
CollectionEntry.javaAPI DocHibernate 3.2.510768Fri Mar 03 20:49:56 GMT 2006org.hibernate.engine

CollectionEntry

public final class CollectionEntry extends Object implements Serializable
We need an entry to tell us all about the current state of a collection with respect to its persistent state
author
Gavin King

Fields Summary
private static final Log
log
private Serializable
snapshot
private String
role
private transient org.hibernate.persister.collection.CollectionPersister
loadedPersister
private Serializable
loadedKey
private transient boolean
reached
private transient boolean
processed
private transient boolean
doupdate
private transient boolean
doremove
private transient boolean
dorecreate
private transient boolean
ignore
private transient org.hibernate.persister.collection.CollectionPersister
currentPersister
private transient Serializable
currentKey
Constructors Summary
public CollectionEntry(org.hibernate.persister.collection.CollectionPersister persister, org.hibernate.collection.PersistentCollection collection)
For newly wrapped collections, or dereferenced collection wrappers


	        	 
	     
		// new collections that get found + wrapped
		// during flush shouldn't be ignored
		ignore = false;

		collection.clearDirty(); //a newly wrapped collection is NOT dirty (or we get unnecessary version updates)
		
		snapshot = persister.isMutable() ?
				collection.getSnapshot(persister) :
				null;
		collection.setSnapshot(loadedKey, role, snapshot);
	
public CollectionEntry(org.hibernate.collection.PersistentCollection collection, org.hibernate.persister.collection.CollectionPersister loadedPersister, Serializable loadedKey, boolean ignore)
For collections just loaded from the database

		this.ignore=ignore;

		//collection.clearDirty()
		
		this.loadedKey = loadedKey;
		setLoadedPersister(loadedPersister);

		collection.setSnapshot(loadedKey, role, null);

		//postInitialize() will be called after initialization
	
public CollectionEntry(org.hibernate.persister.collection.CollectionPersister loadedPersister, Serializable loadedKey)
For uninitialized detached collections

		// detached collection wrappers that get found + reattached
		// during flush shouldn't be ignored
		ignore = false;

		//collection.clearDirty()
		
		this.loadedKey = loadedKey;
		setLoadedPersister(loadedPersister);
	
CollectionEntry(org.hibernate.collection.PersistentCollection collection, SessionFactoryImplementor factory)
For initialized detached collections

		// detached collections that get found + reattached
		// during flush shouldn't be ignored
		ignore = false;

		loadedKey = collection.getKey();
		setLoadedPersister( factory.getCollectionPersister( collection.getRole() ) );

		snapshot = collection.getStoredSnapshot();		
	
private CollectionEntry(String role, Serializable snapshot, Serializable loadedKey, SessionFactoryImplementor factory)
Used from custom serialization.

see
#serialize
see
#deserialize

		this.role = role;
		this.snapshot = snapshot;
		this.loadedKey = loadedKey;
		if ( role != null ) {
			afterDeserialize( factory );
		}
	
Methods Summary
public voidafterAction(org.hibernate.collection.PersistentCollection collection)
Called after execution of an action

		loadedKey = getCurrentKey();
		setLoadedPersister( getCurrentPersister() );
		
		boolean resnapshot = collection.wasInitialized() && 
				( isDoremove() || isDorecreate() || isDoupdate() );
		if ( resnapshot ) {
			snapshot = loadedPersister==null || !loadedPersister.isMutable() ? 
					null : 
					collection.getSnapshot(loadedPersister); //re-snapshot
		}
		
		collection.postAction();
	
voidafterDeserialize(SessionFactoryImplementor factory)

		loadedPersister = factory.getCollectionPersister(role);
	
static org.hibernate.engine.CollectionEntrydeserialize(java.io.ObjectInputStream ois, SessionImplementor session)
Custom deserialization routine used during deserialization of a Session/PersistenceContext for increased performance.

param
ois The stream from which to read the entry.
param
session The session being deserialized.
return
The deserialized CollectionEntry
throws
IOException
throws
ClassNotFoundException

		return new CollectionEntry(
				( String ) ois.readObject(),
		        ( Serializable ) ois.readObject(),
		        ( Serializable ) ois.readObject(),
		        session.getFactory()
		);
	
private voiddirty(org.hibernate.collection.PersistentCollection collection)
Determine if the collection is "really" dirty, by checking dirtiness of the collection elements, if necessary

		
		boolean forceDirty = collection.wasInitialized() &&
				!collection.isDirty() && //optimization
				getLoadedPersister() != null &&
				getLoadedPersister().isMutable() && //optimization
				( collection.isDirectlyAccessible() || getLoadedPersister().getElementType().isMutable() ) && //optimization
				!collection.equalsSnapshot( getLoadedPersister() );
		
		if ( forceDirty ) {
			collection.dirty();
		}
		
	
public java.io.SerializablegetCurrentKey()
This is only available late during the flush cycle

		return currentKey;
	
public org.hibernate.persister.collection.CollectionPersistergetCurrentPersister()

		return currentPersister;
	
public java.io.SerializablegetKey()

		return getLoadedKey();
	
public java.io.SerializablegetLoadedKey()

		return loadedKey;
	
public org.hibernate.persister.collection.CollectionPersistergetLoadedPersister()
This is only available late during the flush cycle

		return loadedPersister;
	
public java.util.CollectiongetOrphans(java.lang.String entityName, org.hibernate.collection.PersistentCollection collection)
Get the collection orphans (entities which were removed from the collection)

		if (snapshot==null) {
			throw new AssertionFailure("no collection snapshot for orphan delete");
		}
		return collection.getOrphans( snapshot, entityName );
	
public java.lang.StringgetRole()

		return role;
	
public java.io.SerializablegetSnapshot()

		return snapshot;
	
public booleanisDorecreate()

		return dorecreate;
	
public booleanisDoremove()

		return doremove;
	
public booleanisDoupdate()

		return doupdate;
	
public booleanisIgnore()

		return ignore;
	
public booleanisProcessed()

		return processed;
	
public booleanisReached()

		return reached;
	
public booleanisSnapshotEmpty(org.hibernate.collection.PersistentCollection collection)

		//TODO: does this really need to be here?
		//      does the collection already have
		//      it's own up-to-date snapshot?
		return collection.wasInitialized() && 
			( getLoadedPersister()==null || getLoadedPersister().isMutable() ) &&
			collection.isSnapshotEmpty( getSnapshot() );
	
public voidpostFlush(org.hibernate.collection.PersistentCollection collection)
Called after a successful flush

		if ( isIgnore() ) {
			ignore = false;
		}
		else if ( !isProcessed() ) {
			throw new AssertionFailure( "collection [" + collection.getRole() + "] was not processed by flush()" );
		}
		collection.setSnapshot(loadedKey, role, snapshot);
	
public voidpostInitialize(org.hibernate.collection.PersistentCollection collection)

		snapshot = getLoadedPersister().isMutable() ?
				collection.getSnapshot( getLoadedPersister() ) :
				null;
		collection.setSnapshot(loadedKey, role, snapshot);
	
public voidpreFlush(org.hibernate.collection.PersistentCollection collection)

		
		boolean nonMutableChange = collection.isDirty() && 
				getLoadedPersister()!=null && 
				!getLoadedPersister().isMutable();
		if (nonMutableChange) {
			throw new HibernateException(
					"changed an immutable collection instance: " + 
					MessageHelper.collectionInfoString( getLoadedPersister().getRole(), getLoadedKey() )
				);
		}
		
		dirty(collection);
		
		if ( log.isDebugEnabled() && collection.isDirty() && getLoadedPersister() != null ) {
			log.debug(
					"Collection dirty: " +
					MessageHelper.collectionInfoString( getLoadedPersister().getRole(), getLoadedKey() )
				);
		}

		setDoupdate(false);
		setDoremove(false);
		setDorecreate(false);
		setReached(false);
		setProcessed(false);
	
voidserialize(java.io.ObjectOutputStream oos)
Custom serialization routine used during serialization of a Session/PersistenceContext for increased performance.

param
oos The stream to which we should write the serial data.
throws
java.io.IOException

		oos.writeObject( role );
		oos.writeObject( snapshot );
		oos.writeObject( loadedKey );
	
public voidsetCurrentKey(java.io.Serializable currentKey)

		this.currentKey = currentKey;
	
public voidsetCurrentPersister(org.hibernate.persister.collection.CollectionPersister currentPersister)

		this.currentPersister = currentPersister;
	
public voidsetDorecreate(boolean dorecreate)

		this.dorecreate = dorecreate;
	
public voidsetDoremove(boolean doremove)

		this.doremove = doremove;
	
public voidsetDoupdate(boolean doupdate)

		this.doupdate = doupdate;
	
private voidsetLoadedPersister(org.hibernate.persister.collection.CollectionPersister persister)

		loadedPersister = persister;
		setRole( persister == null ? null : persister.getRole() );
	
public voidsetProcessed(boolean processed)

		this.processed = processed;
	
public voidsetReached(boolean reached)

		this.reached = reached;
	
public voidsetRole(java.lang.String role)

		this.role = role;
	
public java.lang.StringtoString()

		String result = "CollectionEntry" + 
				MessageHelper.collectionInfoString( loadedPersister.getRole(), loadedKey );
		if (currentPersister!=null) {
			result += "->" + 
					MessageHelper.collectionInfoString( currentPersister.getRole(), currentKey );
		}
		return result;
	
public booleanwasDereferenced()

		return getLoadedKey() == null;