JDO : Auditing

Applicable to RDBMS

With standard JDO you have no annotations available to automativelly add timestamps into the datastore against each record when it is persisted or updated. Whilst you can do this manually, setting the field(s) in prePersist callbacks etc, DataNucleus provides some simple annotations to make it simpler still.

import org.datanucleus.api.jdo.annotations.CreateTimestamp;
import org.datanucleus.api.jdo.annotations.UpdateTimestamp;

@Entity
public class Hotel
{
    @Id
    long id;

    @CreateTimestamp
    Timestamp createTimestamp;

    @UpdateTimestamp
    Timestamp updateTimestamp;

    ...
}

In the above example we have 2 fields in the class that will have columns in the datastore. The field createTimestamp will be persisted at INSERT with the Timestamp of the insert. The field updateTimestamp will be persisted whenever any update is made to the object in the datastore, with the Timestamp of the update.