Cayenne User Documentation
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.

- 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:

Main.java

    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:

Main.java

    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

.
Copyright ©2001-2007 Apache Software Foundation