Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking Changes Using History Policy in eclipse link

I am using eclipse link Customizer to track changes to table:

@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);
}
}

My question : is there a way to fetch this history table using jpa(eclipse link) or i have to make a virtual entity in order to search history.

like image 423
bob-cac Avatar asked Mar 03 '14 07:03

bob-cac


1 Answers

Yes, there is:

javax.persistence.Query historyQuery = em
                .createQuery("SELECT e FROM Employee e", Employee.class)
                .setParameter("id", id)
                .setHint(QueryHints.AS_OF, "yyyy/MM/dd HH:mm:ss.SSS")
                .setHint(QueryHints.READ_ONLY, HintValues.TRUE)
                .setHint(QueryHints.MAINTAIN_CACHE, HintValues.FALSE);

Some things to take care of:

  • The format of the AS_OF date is strict.
  • You should not remove the READ_ONLY and !MAINTAIN_CACHE directives, otherwise it will mess up your persistence unit
  • This does not apply to referenced entities, only to the one returned by the query (Employee in our case)! See my question regarding that: Eclipselink history of related objects
like image 192
pentike Avatar answered Oct 29 '22 16:10

pentike