blob: a591272d52f1843636a8602f9fa965eed1d11586 [file] [log] [blame]
Title: Delete Rules
<H3><A name="DeleteRules-DeleteRules"></A>Delete Rules</H3>
<P>One of the columns in the Modeler ObjRelationships panel is &quot;Delete Rule&quot;. It tells Cayenne what to do with related objects when a <B>source object</B> 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. </P>
<P>Default delete rule is <B>No Action</B>, 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: &quot;Nullify&quot;, &quot;Cascade&quot; and &quot;Deny&quot;. Each one of them is explained below.</P>
<UL>
<LI><B>Nullify Rule:</B> When a <B>source object</B> is deleted, any pointers from the <B>target object(s)</B> 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, &quot;Nullify&quot; 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: <TT>Employee.toDepartment</TT> relationship logically should use &quot;Nullify&quot; rule.</LI>
</UL>
<UL>
<LI><B>Cascade Rule:</B> When a <B>source object</B> is deleted, any of the <B>target object(s)</B> 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: <TT>Department.employeeArray</TT> relationship may use &quot;Cascade&quot; rule.</LI>
</UL>
<UL>
<LI><B>Deny Rule:</B> When a <B>source object</B> is being deleted, but it has at least one <B>target object</B> this rule would generate a runtime exception on delete attempt. This rule is usually a reflection of certain business rules in the application. Example: <TT>Department.employeeArray</TT> relationship may use &quot;Deny&quot; rule if there is an application requirement that explicitly states that non-empty departments can not be deleted.</LI>
</UL>
<DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Cayenne Delete Rules vs. DB Delete Rules</B><BR>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).</TD></TR></TABLE></DIV>