如何使用Spring Data JPA Auditing and Envers跟踪删除事件?

问题描述 投票:0回答:1

在我的审计实体中,我有字段:

@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审核表吗?

spring-data-jpa spring-data hibernate-envers spring-data-envers
1个回答
0
投票

解决此问题:

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);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.