在我的审计实体中,我有字段:
@LastModifiedBy
private String lastModifiedBy;
@LastModifiedDate
private OffsetDateTime lastModifiedDate;
但是删除实体后它们不会改变。据我了解,我需要自定义org.springframework.data.jpa.domain.support.AuditingEntityListener
并在其中添加@PreRemove
,但我不知道如何实现此功能,因为我一直都有
org.hibernate.InstantiationException: Could not instantiate managed bean directly
还有其他选项可跟踪删除事件并将更新的字段存储到Envers审核表吗?
解决此问题:
public class CustomValidityAuditStrategy extends ValidityAuditStrategy {
private final AuditorAware<String> auditorAware = ...;
@Override
public void perform(final Session session, final String entityName, final AuditEntitiesConfiguration audEntitiesCfg, final Serializable id, final Object data, final Object revision) {
if (data instanceof Map) {
final Map dataToUpdate = (Map) data;
dataToUpdate.put("lastModifiedBy", auditorAware.get());
dataToUpdate.put("lastModifiedDate", OffsetDateTime.now());
}
super.perform(session, entityName, audEntitiesCfg, id, data, revision);
}
}