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 cancel its data on the database

You must code a call for the entity instance to cancel its data on the database:

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

For an entity which supports optimistic locking, you must pass the version number held by the client struct.

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:
Figure 2. Incorrect - bypassing optimistic locking safeguards
/** ********** VERY VERY BAD - DO NOT DO THIS! ********** */
    // do the cancel, passing the version number from the entity
    // instance
    someEntity.cancel(someEntity.getVersionNo());
    /** ********** VERY VERY BAD - DO NOT DO THIS! ********** */