{
try {
if (entity == null)
return;
Entity instance = checkEntityType(entity, "remove");
checkTransactionRequired("remove");
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("removing entity class " + instance.getClass().getName() +
" PK: " + instance.__caucho_getPrimaryKey() +
" state: " + instance.__caucho_getEntityState()));
EntityState state = instance.__caucho_getEntityState();
if (EntityState.P_DELETING.ordinal() <= state.ordinal()) {
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("remove is ignoring entity in state " + state));
return;
}
// jpa/0k12
if (instance.__caucho_getConnection() == null) {
if (instance.__caucho_getEntityType() == null) {
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("remove is ignoring entity; performing only cascade post-remove"));
// Ignore this entity; only post-remove child entities.
instance.__caucho_cascadePostRemove(this);
// jpa/0ga7
return;
}
else
throw new IllegalArgumentException(L.l("remove() operation can only be applied to a managed entity. This entity instance '{0}' PK: '{1}' is detached which means it was probably removed or needs to be merged.", instance.getClass().getName(), instance.__caucho_getPrimaryKey()));
}
// jpa/0h25, jpa/0i5e
// Do not flush dependent objects for cascading persistence
// when this entity is being removed.
instance.__caucho_setEntityState(EntityState.P_DELETING);
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("remove is flushing any lazy cascading operation"));
// jpa/1620
// In particular, required for cascading persistence, since the cascade
// is lazy until flush
// jpa/0h25 flushInternal();
// Cannot flush since the delete is lazy until flush, i.e.:
// remove(A); // (*) __caucho_flush()
// remove(B);
// (*) would break a FK constraint if B has a reference to A.
// jpa/0h26
updateFlushPriority(instance);
// jpa/0h25, jpa/0i5e
// Restores original state.
instance.__caucho_setEntityState(state);
Object oldEntity;
oldEntity = getEntity(instance.getClass(),
instance.__caucho_getPrimaryKey());
// jpa/0ga4
if (oldEntity == null)
throw new IllegalArgumentException(L.l("remove() operation can only be applied to a managed entity instance."));
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("remove is performing cascade pre-remove"));
// Pre-remove child entities.
instance.__caucho_cascadePreRemove(this);
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("remove is performing delete on the target entity"));
delete(instance);
// jpa/0o30: flushes the owning side delete.
// XXX: Cannot flush since the delete is lazy until flush.
// jpa/0h25
// instance.__caucho_flush();
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("remove is performing cascade post-remove"));
// jpa/0o30
// Post-remove child entities.
instance.__caucho_cascadePostRemove(this);
if (log.isLoggable(Level.FINER))
log.log(Level.FINER, L.l("DONE successful remove for entity class " +
instance.getClass().getName() +
" PK: " + instance.__caucho_getPrimaryKey()));
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new EJBExceptionWrapper(e);