Jugen commented on code in PR #614:
URL: https://github.com/apache/cayenne/pull/614#discussion_r1595222948


##########
cayenne/src/main/java/org/apache/cayenne/access/flush/RootRowOpProcessor.java:
##########
@@ -74,14 +79,37 @@ public Void visitUpdate(UpdateDbRowOp dbRow) {
 
     @Override
     public Void visitDelete(DeleteDbRowOp dbRow) {
-        if (dbRowOpFactory.getDescriptor().getEntity().isReadOnly()) {
+        ObjEntity entity = dbRowOpFactory.getDescriptor().getEntity();
+        if (entity.isReadOnly()) {
             throw new CayenneRuntimeException("Attempt to modify object(s) 
mapped to a read-only entity: '%s'. " +
-                    "Can't commit changes.", 
dbRowOpFactory.getDescriptor().getEntity().getName());
+                    "Can't commit changes.", entity.getName());
         }
         diff.apply(deleteHandler);
-        Collection<ObjectId> flattenedIds = 
dbRowOpFactory.getStore().getFlattenedIds(dbRow.getChangeId());
-        flattenedIds.forEach(id -> 
dbRowOpFactory.getOrCreate(dbRowOpFactory.getDbEntity(id), id, 
DbRowOpType.DELETE));
-        if (dbRowOpFactory.getDescriptor().getEntity().getDeclaredLockType() 
== ObjEntity.LOCK_TYPE_OPTIMISTIC) {
+
+        DbEntity dbSource = entity.getDbEntity();
+        Set<Entry<CayennePath,ObjectId>> flattenedEntries = 
dbRowOpFactory.getStore().getFlattenedEntries(dbRow.getChangeId());
+
+        flattenedEntries.forEach(entry -> {
+            DbRelationship dbRel = 
dbSource.getRelationship(entry.getKey().first().toString());
+
+            // Don't delete if the target entity has a toMany relationship 
with the source entity,
+            // as there may be other records in the source entity with 
references to it.
+            if (!dbRel.getReverseRelationship().isToMany()) {
+
+                // Check if there's an ObjRelationship matching the flattened 
attributes DbRelationship
+                boolean hasObjRelationship = entity.getRelationships().stream()

Review Comment:
   I'd prefer this being backported to 4.2 if possible.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cayenne.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to