public void execute(EntityEnterpriseContext ctx) throws RemoveException, RemoteException {
if(entity.isRemoved(ctx))
{
throw new IllegalStateException("Instance was already removed: id=" + ctx.getId());
}
entity.setIsBeingRemoved(ctx);
// remove entity from all relations
Object[] oldRelationsRef = new Object[1];
boolean needsSync = entity.removeFromRelations(ctx, oldRelationsRef);
// update the related entities (stores the removal from relationships)
// if one of the store fails an EJBException will be thrown
if(!syncOnCommitOnly && needsSync)
{
EntityContainer.synchronizeEntitiesWithinTransaction(ctx.getTransaction());
}
if(!batchCascadeDelete)
{
if(!entity.isScheduledForBatchCascadeDelete(ctx))
{
executeDeleteSQL(ctx);
}
else
{
if(log.isTraceEnabled())
log.trace("Instance is scheduled for cascade delete. id=" + ctx.getId());
}
}
// cascate-delete to old relations, if relation uses cascade.
if(oldRelationsRef[0] != null)
{
Map oldRelations = (Map)oldRelationsRef[0];
entity.cascadeDelete(ctx, oldRelations);
}
if(batchCascadeDelete)
{
if(!entity.isScheduledForBatchCascadeDelete(ctx))
{
executeDeleteSQL(ctx);
}
else
{
if(log.isTraceEnabled())
log.debug("Instance is scheduled for cascade delete. id=" + ctx.getId());
}
}
entity.setRemoved(ctx);
manager.getReadAheadCache().removeCachedData(ctx.getId());
}
|