One of the columns in the Modeler ObjRelationships panel is "Delete Rule". It tells Cayenne what to do with related objects when a source object of the relationship is deleted. Cayenne will use this delete rule to perform cleanup of other objects after a given object was deleted from the graph.
Default delete rule is No Action, meaning just that - when an object is deleted, no changes should happen to objects related to this object via a given relationship. Though this is a default, it is not very useful, and can lead to object graph corruption in many cases. It is developer responsibility to select a more meaningful delete rule. The choices are: "Nullify", "Cascade" and "Deny". Each one of them is explained below.
- Nullify Rule: When a source object is deleted, any pointers from the target object(s) back to the deleted object will be reset to null. This presumes an existence of reverse ObjRelationship for a given ObjRelationship. If no such reverse relationship exist, "Nullify" rule has no effect. This rule should be used when target object(s) of relationship can exist without the source object and do not depend on it. Example: Employee.toDepartment relationship logically should use "Nullify" rule.
- Cascade Rule: When a source object is deleted, any of the target object(s) will be deleted too. This may cause the whole tree of objects to be deleted with one command. This rule should be used when target object(s) of relationship can not exist without the source object. Example: Department.employeeArray relationship may use "Cascade" rule.
- Deny Rule: When a source object is being deleted, but it has at least one target object this rule would generate a runtime exception on delete attempt. This rule is usually a reflection of certain business rules in the application. Example: Department.employeeArray relationship may use "Deny" rule if there is an application requirement that explicitly states that non-empty departments can not be deleted.
|Cayenne Delete Rules vs. DB Delete Rules|
Some databases allow to define delete rules logically similar to the rules described above. Cayenne rules differ from DB rules in that they deal with object graph instead of relational schema. Some changes to the object graph will result in database operations (e.g. resetting FK to NULL as a result of Nullify rule), some others will not (e.g. removing a deleted DataObject from the to-many array as a result of another Nullify rule).