mercredi 25 février 2015

Easiest way to list entity class field-level deltas between last saved and new



I have a requirement by which I have to detect and save field-level deltas between the last saved and new changes of an entity class, which is modified through a JSF front end. IOW, the form, entity class, and the DB table may have X number of fields and on every save, anywhere between 0 and X can be changed. I need to find on every save all the ones that have changes and log them somewhere for audit purposes.


At first I thought of modifying each setter:



@Transient
private Set<String> changes;

...

public void setSomeField(String _newval) {

if(!_newval.equals(this.someField)){
this.changes.add("SOME_FIELD");
}

this.someField = _newval;
}


which would require this non-elegant delta detection logic to be implemented in each setter and I don't like it. Is there an easy way to diff the new state from the persisted state using some JPA function that would give me a listing of all the fields changes and that I can then dump into my audit log storage?




Aucun commentaire:

Enregistrer un commentaire