Before we discuss the API for object deletion, lets go back to CayenneModeler and set up some delete rules. Doing this is optional but will simplify correct handling of the objects related to deleted objects.
- In the Modeler go to "Artist" ObjEntity, "Relationships" tab and select "Cascade" for the "paintings" relationship delete rule:
- Repeat this step for Gallery (set "paintings" relationship to be "Cascade")
- Repeat this step for Painting (set both relationships rules to "Nullify").
- Save the mapping, and refresh the porject in Eclispe.
- To delete an object we first need to get a hold of this object. Let's use utility class DataObjectUtils to find an artist:
Now let's delete the artist:
Since we set up "Cascade" delete rule for the Artist.paintings relationships, Cayenne will automatically delete all paintings of this artist. So when your run the app you'll see this output:
Next Step: Tutorial Webapp