EclipseLink/Examples/JPA/SoftDelete

Revision as of 11:19, 11 May 2011 by James.sutherland.oracle.com(Talk | contribs)(New page: SoftDelete __TOC__ Some applications prefer to archive deleted objects instead of deleting them. This can be desired for auditing, or backup purposes...)

Contents

Some applications prefer to archive deleted objects instead of deleting them. This can be desired for auditing, or backup purposes. This is sometimes referred to as "soft" deletes, in that rows are never deleted, just marked as delete through a status column in the table.

To implement soft deletes you need a status column in your table, in this example we will use STATUS.
You can define an attribute in your entity to map to the status, or leave it as un-mapped if desired. If un-mapped, you will need to add a QueryKey for it to query on it through JPQL.

Next, you will need to override the delete operation for your entity to instead update the status field to 'deleted'. This can be done through using a DescriptorCustomizer and using the DescriptorQueryManager setDeleteSQLString() API.

Finally, to avoid having deleted object returned on queries, you can configure an @AdditionalCriteria to exclude the values.

Employee entity using soft deletes

@Entity
@AdditionalCriteria("this.status <> 'deleted'")publicclass Employee {
@Id
@Column(name="EMP_ID")privatelong id;
@Basic
privateString status;// This is optional, as a query-key could also be used.}