EclipseLink/Examples/JPA/History

From Eclipsepedia

< EclipseLink‎ | Examples‎ | JPA
Revision as of 13:45, 15 June 2010 by James.sutherland.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


EclipseLink provides extended support for tracking all changes made to the database. The EclipseLink HistoryPolicy can be configured on a ClassDescriptor to store a mirror table of the original that will store the state of the object at any point in time. This can be used for auditing purposes, or to allow querying as of past points in time, or to allow restoring old data.

When enabled EclipseLink will insert a row into the history table on every update to the primary table to track the rows state. The history table also defines a start and end timestamp, so that the state of any row at any point in time can be determined.

EclipseLink also supports queries as of a past point in time, either through using EclipseLink Expressions, or through using the "eclipselink.history.as-of" query hint, or an EclipseLink HistoricalSession.

EclipseLink also supports historical queries on database that are enabled with Oracle Flashback.

Contents


Example: Enabling history

History support is enabled for the Employee class.

@Entity
@Customizer(org.acme.persistence.HistoryCustomizer.class)
public class Employee{
    @Id
    private long id;
    ...
}
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.descriptors.history.HistoryPolicy;
 
public class HistoryCustomizer implements DescriptorCustomizer {
 
    public void customize(ClassDescriptor descriptor) {
        HistoryPolicy policy = new HistoryPolicy();
        policy.addHistoryTableName("EMPLOYEE_HIST");
        policy.addStartFieldName("START_DATE");
        policy.addEndFieldName("END_DATE");
        descriptor.setHistoryPolicy(policy);
    }
}

Example history table

The state of the EMPLOYEE_HIST table will mirror the EMPLOYEE, but with multiple rows for each current row, and history for deleted rows.