EntityBeanIntercept.isOrphanDelete() - Additional refactor for #1988

This commit is contained in:
rob bygrave
2020-04-14 23:17:56 +12:00
parent c9588586c9
commit cdc5eefc69
2 changed files with 17 additions and 27 deletions
@@ -1140,6 +1140,17 @@ public final class EntityBeanIntercept implements Serializable {
this.sortOrder = sortOrder;
}
/**
* Set if the entity was deleted from a BeanCollection.
*/
public void setDeletedFromCollection(final boolean deletedFromCollection) {
this.deletedFromCollection = deletedFromCollection;
}
public boolean isOrphanDelete() {
return deletedFromCollection && !isNew();
}
/**
* Set the load error that happened on this property.
*/
@@ -1171,18 +1182,4 @@ public final class EntityBeanIntercept implements Serializable {
}
return ret;
}
/**
* Returns true if the entity was removed from a BeanCollection. This can be used to track movement from one collection to another.
*/
public boolean isDeletedFromCollection() {
return deletedFromCollection;
}
/**
* Set if the entity was deleted from a BeanCollection.
*/
public void setDeletedFromCollection(final boolean deletedFromCollection) {
this.deletedFromCollection = deletedFromCollection;
}
}
@@ -70,9 +70,8 @@ public class SaveManyBeans extends SaveManyBase {
}
} else {
if (isModifyListenMode() || many.hasOrderColumn()) {
// delete any removed beans via private owned. Needs to occur before
// a 'deleteMissingChildren' statement occurs
removeAssocManyPrivateOwned();
// delete any removed beans / orphans
removeAssocManyOrphans();
}
if (cascade) {
// potentially deletes 'missing children' for 'stateless update'
@@ -340,20 +339,16 @@ public class SaveManyBeans extends SaveManyBase {
transaction.depth(-1);
}
private void removeAssocManyPrivateOwned() {
private void removeAssocManyOrphans() {
// check that the list is not null and if it is a BeanCollection
// check that is has been populated (don't trigger lazy loading)
if (value instanceof BeanCollection<?>) {
BeanCollection<?> c = (BeanCollection<?>) value;
Set<?> modifyRemovals = c.getModifyRemovals();
if (insertedParent) {
// after insert set the modify listening mode for private owned etc
c.setModifyListening(many.getModifyListenMode());
}
// We must not reset when we still have to update other entities in the collection and set their new orderColumn value
if (!many.hasOrderColumn()) {
c.modifyReset();
@@ -362,11 +357,9 @@ public class SaveManyBeans extends SaveManyBase {
for (Object removedBean : modifyRemovals) {
if (removedBean instanceof EntityBean) {
EntityBean eb = (EntityBean) removedBean;
if (!eb._ebean_getIntercept().isNew()) {
if (eb._ebean_intercept().isDeletedFromCollection()) {
// only delete if the bean was loaded meaning that it is known to exist in the DB
persister.deleteRequest(persister.createDeleteRemoved(removedBean, transaction, request.getFlags()));
}
if (eb._ebean_intercept().isOrphanDelete()) {
// only delete if the bean was loaded meaning that it is known to exist in the DB
persister.deleteRequest(persister.createDeleteRemoved(removedBean, transaction, request.getFlags()));
}
}
}