| Method from org.hibernate.collection.AbstractPersistentCollection Detail: |
public boolean afterInitialize() {
setInitialized();
//do this bit after setting initialized to true or it will recurse
if (operationQueue!=null) {
performQueuedOperations();
operationQueue=null;
cachedSize = -1;
return false;
}
else {
return true;
}
}
|
public void afterRowInsert(CollectionPersister persister,
Object entry,
int i) throws HibernateException {
}
Called after inserting a row, to fetch the natively generated id |
public void beginRead() {
// override on some subclasses
initializing = true;
}
Called just before reading any rows from the JDBC result set |
public final void clearDirty() {
dirty = false;
}
|
public final void dirty() {
dirty = true;
}
|
abstract public boolean empty()
Is the initialized collection empty? |
public boolean endRead() {
//override on some subclasses
return afterInitialize();
}
Called after reading all rows from the JDBC result set |
public final void forceInitialization() throws HibernateException {
if (!initialized) {
if (initializing) {
throw new AssertionFailure("force initialize loading collection");
}
if (session==null) {
throw new HibernateException("collection is not associated with any session");
}
if ( !session.isConnected() ) {
throw new HibernateException("disconnected session");
}
session.initializeCollection(this, false);
}
}
To be called internally by the session, forcing
immediate initialization. |
protected int getCachedSize() {
return cachedSize;
}
|
public Object getIdentifier(Object entry,
int i) {
throw new UnsupportedOperationException();
}
|
public final Serializable getKey() {
return key;
}
|
abstract public Collection getOrphans(Serializable snapshot,
String entityName) throws HibernateException
get all "orphaned" elements |
protected static Collection getOrphans(Collection oldElements,
Collection currentElements,
String entityName,
SessionImplementor session) throws HibernateException {
// short-circuit(s)
if ( currentElements.size()==0 ) return oldElements; // no new elements, the old list contains only Orphans
if ( oldElements.size()==0) return oldElements; // no old elements, so no Orphans neither
Type idType = session.getFactory().getEntityPersister(entityName).getIdentifierType();
// create the collection holding the Orphans
Collection res = new ArrayList();
// collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
java.util.Set currentIds = new HashSet();
for ( Iterator it=currentElements.iterator(); it.hasNext(); ) {
Object current = it.next();
if ( current!=null && ForeignKeys.isNotTransient(entityName, current, null, session) ) {
Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, current, session);
currentIds.add( new TypedValue( idType, currentId, session.getEntityMode() ) );
}
}
// iterate over the *old* list
for ( Iterator it=oldElements.iterator(); it.hasNext(); ) {
Object old = it.next();
Serializable oldId = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, old, session);
if ( !currentIds.contains( new TypedValue( idType, oldId, session.getEntityMode() ) ) ) {
res.add(old);
}
}
return res;
}
Given a collection of entity instances that used to
belong to the collection, and a collection of instances
that currently belong, return a collection of orphans |
public Object getOwner() {
return owner;
}
|
public final Collection getQueuedOrphans(String entityName) {
if ( hasQueuedOperations() ) {
Collection additions = new ArrayList( operationQueue.size() );
Collection removals = new ArrayList( operationQueue.size() );
for ( int i = 0; i < operationQueue.size(); i++ ) {
DelayedOperation op = (DelayedOperation) operationQueue.get(i);
additions.add( op.getAddedInstance() );
removals.add( op.getOrphan() );
}
return getOrphans(removals, additions, entityName, session);
}
else {
return CollectionHelper.EMPTY_COLLECTION;
}
}
Iterate the "queued" additions |
public final String getRole() {
return role;
}
|
public final SessionImplementor getSession() {
return session;
}
|
protected final Serializable getSnapshot() {
return session.getPersistenceContext().getSnapshot(this);
}
Get the current snapshot from the session |
public final Serializable getStoredSnapshot() {
return storedSnapshot;
}
|
public Object getValue() {
return this;
}
return the user-visible collection (or array) instance |
public final boolean hasQueuedOperations() {
return operationQueue!=null;
}
Does this instance have any "queued" additions? |
static void identityRemove(Collection list,
Object object,
String entityName,
SessionImplementor session) throws HibernateException {
if ( object!=null && ForeignKeys.isNotTransient(entityName, object, null, session) ) {
Type idType = session.getFactory().getEntityPersister(entityName).getIdentifierType();
Serializable idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, object, session);
Iterator iter = list.iterator();
while ( iter.hasNext() ) {
Serializable idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved(entityName, iter.next(), session);
if ( idType.isEqual( idOfCurrent, idOfOld, session.getEntityMode(), session.getFactory() ) ) {
iter.remove();
break;
}
}
}
}
|
protected final void initialize(boolean writing) {
if (!initialized) {
if (initializing) {
throw new LazyInitializationException("illegal access to loading collection");
}
throwLazyInitializationExceptionIfNotConnected();
session.initializeCollection(this, writing);
}
}
Initialize the collection, if possible, wrapping any exceptions
in a runtime exception |
protected boolean isClearQueueEnabled() {
return !initialized &&
isConnectedToSession() &&
isInverseCollectionNoOrphanDelete();
}
Is this collection in a state that would allow us to
"queue" clear? This is a special case, because of orphan
delete. |
public boolean isDirectlyAccessible() {
return directlyAccessible;
}
Could the application possibly have a direct reference to
the underlying collection implementation? |
public final boolean isDirty() {
return dirty;
}
|
protected boolean isOperationQueueEnabled() {
return !initialized &&
isConnectedToSession() &&
isInverseCollection();
}
Is this collection in a state that would allow us to
"queue" operations? |
protected boolean isPutQueueEnabled() {
return !initialized &&
isConnectedToSession() &&
isInverseOneToManyOrNoOrphanDelete();
}
Is this collection in a state that would allow us to
"queue" puts? This is a special case, because of orphan
delete. |
public boolean isRowUpdatePossible() {
return true;
}
|
public final boolean isUnreferenced() {
return role==null;
}
|
public boolean needsRecreate(CollectionPersister persister) {
return false;
}
Do we need to completely recreate this collection when it changes? |
protected final void performQueuedOperations() {
for ( int i=0; i< operationQueue.size(); i++ ) {
( (DelayedOperation) operationQueue.get(i) ).operate();
}
}
After reading all existing elements from the database,
add the queued elements to the underlying collection. |
public void postAction() {
operationQueue=null;
cachedSize = -1;
clearDirty();
}
After flushing, clear any "queued" additions, since the
database state is now synchronized with the memory state. |
public void preInsert(CollectionPersister persister) throws HibernateException {
}
Called before inserting rows, to ensure that any surrogate keys
are fully generated |
protected final void queueOperation(Object element) {
if (operationQueue==null) operationQueue = new ArrayList(10);
operationQueue.add(element);
dirty = true; //needed so that we remove this collection from the second-level cache
}
|
public final Iterator queuedAdditionIterator() {
if ( hasQueuedOperations() ) {
return new Iterator() {
int i = 0;
public Object next() {
return ( (DelayedOperation) operationQueue.get(i++) ).getAddedInstance();
}
public boolean hasNext() {
return i< operationQueue.size();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
else {
return EmptyIterator.INSTANCE;
}
}
Iterate the "queued" additions |
protected final void read() {
initialize(false);
}
Called by any read-only method of the collection interface |
protected Object readElementByIndex(Object index) {
if (!initialized) {
throwLazyInitializationExceptionIfNotConnected();
CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(this);
CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) {
if ( hasQueuedOperations() ) {
session.flush();
}
return persister.getElementByIndex( entry.getLoadedKey(), index, session, owner );
}
}
read();
return UNKNOWN;
}
|
protected Boolean readElementExistence(Object element) {
if (!initialized) {
throwLazyInitializationExceptionIfNotConnected();
CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(this);
CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) {
if ( hasQueuedOperations() ) {
session.flush();
}
return new Boolean( persister.elementExists( entry.getLoadedKey(), element, session ) );
}
}
read();
return null;
}
|
protected Boolean readIndexExistence(Object index) {
if (!initialized) {
throwLazyInitializationExceptionIfNotConnected();
CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(this);
CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) {
if ( hasQueuedOperations() ) {
session.flush();
}
return new Boolean( persister.indexExists( entry.getLoadedKey(), index, session ) );
}
}
read();
return null;
}
|
protected boolean readSize() {
if (!initialized) {
if ( cachedSize!=-1 && !hasQueuedOperations() ) {
return true;
}
else {
throwLazyInitializationExceptionIfNotConnected();
CollectionEntry entry = session.getPersistenceContext().getCollectionEntry(this);
CollectionPersister persister = entry.getLoadedPersister();
if ( persister.isExtraLazy() ) {
if ( hasQueuedOperations() ) {
session.flush();
}
cachedSize = persister.getSize( entry.getLoadedKey(), session );
return true;
}
}
}
read();
return false;
}
Called by the size() method |
public final boolean setCurrentSession(SessionImplementor session) throws HibernateException {
if (session==this.session) {
return false;
}
else {
if ( isConnectedToSession() ) {
CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(this);
if (ce==null) {
throw new HibernateException(
"Illegal attempt to associate a collection with two open sessions"
);
}
else {
throw new HibernateException(
"Illegal attempt to associate a collection with two open sessions: " +
MessageHelper.collectionInfoString(
ce.getLoadedPersister(),
ce.getLoadedKey(),
session.getFactory()
)
);
}
}
else {
this.session = session;
return true;
}
}
}
Associate the collection with the given session. |
protected final void setDirectlyAccessible(boolean directlyAccessible) {
this.directlyAccessible = directlyAccessible;
}
|
protected final void setInitialized() {
this.initializing = false;
this.initialized = true;
}
|
public void setOwner(Object owner) {
this.owner = owner;
}
|
public void setSnapshot(Serializable key,
String role,
Serializable snapshot) {
this.key = key;
this.role = role;
this.storedSnapshot = snapshot;
}
After flushing, re-init snapshot state. |
public final boolean unsetSession(SessionImplementor currentSession) {
if (currentSession==this.session) {
this.session=null;
return true;
}
else {
return false;
}
}
Disassociate this collection from the given session. |
public final boolean wasInitialized() {
return initialized;
}
Is this instance initialized? |
protected final void write() {
initialize(true);
dirty();
}
Called by any writer method of the collection interface |