Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA : Ideas to track the evolution/changes of the entities

Tags:

java

jpa

I was wondering is there any easy way to implement the tracking of changes in the entities ? There is Envers from Hibernate to have an audit, but as far as I understood it's Hibernate oriented. I am thinking if there is something inside JPA, or a solution which does not get outside of the spec. If there isn't made any, could somebody maybe throw me an idea how to get started with this kind of thing. One idea which comes to me is to create an entity for example :

class Change {
  String className;
  long id;
  String fieldName;
  String fieldValue;
  Date dateOfChange;
}

Which would contain the changed properties. This solution seems to be quite efficient in terms of storage place, but it could be more difficult to handle relations between entities which are tracked ( did not figured it out yet ).

I greatly appreciate any input in this topic,

Kind regards, P.

like image 712
redbull Avatar asked Jan 28 '11 07:01

redbull


1 Answers

You can use the @PrePersist Annotation to create an Entry and store it to the Database.

class Entity{

    @PrePersist
    public void logChanges(){
        Change c = new Change()
        c.setEverythingXouLikeToLog();

        entityManager.persist(c);
        // don't forget ExceptionHandling
    }

}
like image 57
Christian Kuetbach Avatar answered Oct 27 '22 01:10

Christian Kuetbach