6

I am using hibernate-envers with spring. Everything works just fine, except when I delete an entity, it does not change the values of updated_by and updated_date inside audit table, instead it saves an entity exactly as it was before (just copy) after spring.jpa.properties.org.hibernate.envers.store_data_at_delete=true.

I have already tried to register listener EventType.PRE_DELETE, but it didn't help.

Here is my UpdateEntity:

@LastModifiedBy
@Column(nullable = false)
private Long updatedBy;

@LastModifiedDate
@Column(nullable = false)
private Date updatedDate;

How can I capture who deleted and when was deleted inside audit table by modifying columns updated_by and updated_date?

kzharas210
  • 440
  • 1
  • 5
  • 13

1 Answers1

6

Looks like you are combining to features: Auditing support and Envers.

Envers records every change to your entity including the delete if configured as you did.

Auditing support traces update and insert timestamps and users. It doesn't work for deletes since there would be nowhere to store that information.

If you want to keep the combination and still make it work you'd need to modify the entity (e.g. setting an extra flag deleting to true), flush it, maybe even commit it (I'm not sure if that is necessary) and then delete it.

Probably a better approach is using a custom revision object and store the required data there as described in this question and answer: Ways to pass additional data to Custom RevisionEntity in Hibernate Envers?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • That is exactly what I was doing - *Combining Auditing and Envers*, which is wrong. I have done it by creating custom revision object. Thnx a lot – kzharas210 Nov 13 '19 at 11:06
  • Auditing seems to be covered by Envers pretty fine if you use soft-deletion instead of hard deleting the records from the database. – RAM237 Jan 12 '23 at 10:07