The solution

Coding the solution involves these steps:

Create a class variable to hold the DAO

This step is identical to that in You want to read some data from a database table above.

Use the DAO to retrieve the instance of the entity

This step is identical to that in You want to read some data from a database table above.

Instruct the entity instance to remove its data from the database

You must code a call for the entity instance to remove its data from the database:

Figure 1. Calling the remove persistence method on an entity
// do the remove, passing the version number from the client
    someEntity.remove(key.versionNo);
The entity instance will:
  • perform cross-entity validation, allowing other entities to veto the removal; and
  • remove its data from the database.

For an entity which supports optimistic locking, you must pass the version number held by the client struct. Note that this approach is stricter than the classic Cúram approach which does not require a version number.

Important: Do not be tempted to use the version number on the entity instance which has been retrieved, as this would render the optimistic lock mechanism useless and allow one user's updates to be removed by another user acting on out-of-date data:
Figure 2. Incorrect - bypassing optimistic locking safeguards
/** ********** VERY VERY BAD - DO NOT DO THIS! ********** */
    // do the remove, passing the version number from the entity
    // instance
    someEntity.remove(someEntity.getVersionNo());
    /** ********** VERY VERY BAD - DO NOT DO THIS! ********** */