Title: Tutorial Delete
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.
Expression qualifier = ExpressionFactory.matchExp(Artist.NAME_PROPERTY, "Pablo Picasso"); SelectQuery select = new SelectQuery(Artist.class, qualifier); Artist picasso = (Artist) DataObjectUtils.objectForQuery(context, select);
Now let's delete the artist:
if (picasso != null) { context.deleteObject(picasso); context.commitChanges(); }
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:
INFO QueryLogger: --- will run 2 queries. INFO QueryLogger: --- transaction started. INFO QueryLogger: DELETE FROM PAINTING WHERE ID = ? INFO QueryLogger: [bind: 361] INFO QueryLogger: === updated 1 row. INFO QueryLogger: [bind: 360] INFO QueryLogger: === updated 1 row. INFO QueryLogger: DELETE FROM ARTIST WHERE ID = ? INFO QueryLogger: [bind: 360] INFO QueryLogger: === updated 1 row. INFO QueryLogger: +++ transaction committed.
Next Step: Tutorial Webapp