0

We have an application that tracks all the  entity changes with bi-temporal data. Each entity has:

@Embeddable 
public class AuditInfo {
    private Date effectiveFrom;
    private Date effectiveTo;
    private Date asOf;
    private Boolean isCurrent;
}

However on update of an entity we would like to insert a new record and update the old one with isCurrent = false and update the effectiveTo date with current date time.

Could I achieve this with Spring Jpa-Envers?

I tried wiring Hibernate Interceptors and later realised I can only modified the entity fields from within the interceptor and can’t do anything more than that.

I can easily add a service layer that will achieve this but it sounds a like it don’t belong in a service as it a cross cutting concern.

The other that we are toying with is a have custom repository and overriding the save() method.

Would appreciate your thoughts.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
A.U
  • 51
  • 6

1 Answers1

0

Old versions of entities in Envers are stored in a separate table. Hence flags as isCurrent make no sense, as what's in the main table is the current version. If the effectiveTo/effectiveFrom fields are changed, new revisions will be inserted to the audit table.

Now, if entities with different effective dates have different identities, then you should simply model it as separate entities, and just you JPA.

If the identity remains the same, it's a great use-case for Envers.

adamw
  • 8,038
  • 4
  • 28
  • 32
  • When we create entities the effectiveTo date is something in the future (+100 years). When an entity is modified we want to update the old version with effectiveTo=sysdate-1 and the new version with effectiveFrom=sysdate. This makes it easier to query for an entity for a given date for which it was effective between. It can also be achieved with only having effectiveFrom on the entity but means the query is much more complicated. Can the previous revsion be updated? – A.U Sep 04 '14 at 17:15
  • No, revisions are immutable. But it seems to me that in your case different versions of entities have different identities, so you should simply model it as separate entities, and query using JPA. Envers won't give you much in this case. – adamw Sep 05 '14 at 18:08
  • As historical versions are stored in a seperate table and when the current version gets moved across there must be way to hook into this to modifiy revision data? – A.U Sep 05 '14 at 18:44
  • You could hack around this by overriding Envers's listeners, but there's no "official" way to do that. – adamw Sep 08 '14 at 15:43
  • Hi Adam, I like the functionality of what the class ValidityAuditStrategy provides but would be better if there was a way to dates for start-revision and end-revision, is this possible? – A.U Oct 03 '14 at 12:17
  • The dates are stored indirectly in the revision entities. You can have two revisions with the same date quite easily. – adamw Oct 06 '14 at 07:05