开发者

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

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.


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
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜