【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了Cascade.java相关资料,希望对各位网友有所帮助!
//$Id: Cascade.java 7652 2005-07-26 05:51:47Z oneovthafew $
package org.hibernate.engine;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.event.EventSource;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.AbstractComponentType;
import org.hibernate.type.AssociationType;
import org.hibernate.type.CollectionType;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.hibernate.util.CollectionHelper;
/**
* Implements cascaded save / delete / update / lock / evict / replicate / persist / merge
*
* @see org.hibernate.type.AssociationType
* @author Gavin King
*/
public final class Cascade {
private int cascadeTo;
private EventSource eventSource;
private CascadingAction action;
public Cascade(
final CascadingAction action,
final int cascadeTo,
final EventSource eventSource
) {
this.cascadeTo = cascadeTo;
this.eventSource = eventSource;
this.action = action;
}
private static final Log log = LogFactory.getLog(Cascade.class);
// The available cascade actions:
/**
* A cascade point that occurs just after the insertion of the parent entity and
* just before deletion
*/
public static final int AFTER_INSERT_BEFORE_DELETE = 1;
/**
* A cascade point that occurs just before the insertion of the parent entity and
* just after deletion
*/
public static final int BEFORE_INSERT_AFTER_DELETE = 2;
/**
* A cascade point that occurs just after the insertion of the parent entity and
* just before deletion, inside a collection
*/
public static final int AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION = 3;
/**
* A cascade point that occurs just after update of the parent entity
*/
public static final int AFTER_UPDATE = 0;
/**
* A cascade point that occurs just before the session is flushed
*/
public static final int BEFORE_FLUSH = 0;
/**
* A cascade point that occurs just after eviction of the parent entity from the
* session cache
*/
public static final int AFTER_EVICT = 0;
/**
* A cascade point that occurs just after locking a transient parent entity into the
* session cache
*/
public static final int BEFORE_REFRESH = 0;
/**
* A cascade point that occurs just after refreshing a parent entity
*/
public static final int AFTER_LOCK = 0;
/**
* A cascade point that occurs just before merging from a transient parent entity into
* the object in the session cache
*/
public static final int BEFORE_MERGE = 0;
// The allowable cascade styles for a property:
/**
* Cascade an action to the child or children
*/
private void cascadeProperty(
final Object child,
final Type type,
final CascadeStyle style,
final Object anything,
final boolean isCascadeDeleteEnabled)
throws HibernateException {
if (child!=null) {
if ( type.isAssociationType() ) {
AssociationType associationType = (AssociationType) type;
if ( cascadeAssociationNow( associationType ) ) {
cascadeAssociation(
child,
type,
style,
anything,
isCascadeDeleteEnabled
);
}
}
else if ( type.isComponentType() ) {
cascadeComponent( child, (AbstractComponentType) type, anything );
}
}
}
private boolean cascadeAssociationNow(AssociationType associationType) {
return associationType.getForeignKeyDirection().cascadeNow(cascadeTo) &&
( eventSource.getEntityMode()!=EntityMode.DOM4J || associationType.isEmbeddedInXML() );
}
private void cascadeComponent(
final Object child,
final AbstractComponentType componentType,
final Object anything
) {
Object[] children = componentType.getPropertyValues(child, eventSource);
Type[] types = componentType.getSubtypes();
for ( int i=0; i<types.length; i++ ) {
CascadeStyle componentPropertyStyle = componentType.getCascadeStyle(i);
if ( componentPropertyStyle.doCascade(action) ) {
cascadeProperty(
children[i],
types[i],
componentPropertyStyle,
anything,
false
);
}
}
}
private void cascadeAssociation(
final Object child,
final Type type,
final CascadeStyle style,
final Object anything,
final boolean isCasca