在Spring Data JPA中进行审计和@Embedded

时间:2016-07-07 11:38:36

标签: spring jpa spring-data-jpa

我遇到了JPA审核和@Embedded成员的问题。请考虑以下示例场景:

我在Oracle DB中设置了一个测试表:

CREATE TABLE AUDIT_TEST (
  ID            NUMBER(38)   NOT NULL PRIMARY KEY,
  CREATION_DATE TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL
);

我使用审核来定义JPA @Entity

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "AUDIT_TEST")
public class AuditTest {

  private Long id;
  private LocalDateTime creationDate;

  @Id
  @Column(name = "ID")
  public Long getId() { return id; }

  public void setId(Long id) { this.id = id; }

  @CreatedDate
  @Column(name = "CREATION_DATE")
  public LocalDateTime getCreationDate() { return creationDate; }

  public void setCreationDate(LocalDateTime creationDate) {
    this.creationDate = creationDate;
  }

}

最后,我在@Configuration中启用了JPA审核:

@SpringBootApplication()
@EnableJpaAuditing()
public class AuditTestApplication {
}

到目前为止一切顺利;当我构造一个AuditTest实例,为它分配一个id并提交时,creationDate列会按预期填充当前时间戳。

但是,当我将审计列封装在@Embeddable

中时,事情就会停止工作
@Embeddable
public class AuditTestEmbeddable {

  private LocalDateTime creationDate;

  @CreatedDate
  @Column(name = "CREATION_DATE")
  public LocalDateTime getCreationDate() { return creationDate; }

  public void setCreationDate(LocalDateTime creationDate) {
    this.creationDate = creationDate;
  }

}

然后我更改我的实体类以嵌入创建日期:

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "AUDIT_TEST")
public class AuditTest {

  private Long id;
  private AuditTestEmbeddable auditTestEmbeddable = new AuditTestEmbeddable();

  @Id
  @Column(name = "ID")
  public Long getId() { return id; }

  public void setId(Long id) { this.id = id; }

  @Embedded
  public AuditTestEmbeddable getAuditTestEmbeddable() {
    return auditTestEmbeddable;
  }

  public void setAuditTestEmbeddable(AuditTestEmbeddable auditTestEmbeddable) {
    this.auditTestEmbeddable = auditTestEmbeddable;
  }

}

不幸的是,审计工作不再有效。

是否有人知道在使用@Embedded课程时保存审核功能的方法?

2 个答案:

答案 0 :(得分:2)

<强>更新 此功能已添加到Spring Data 2.1 M2(Lovelace)中。 https://jira.spring.io/browse/DATACMNS-1274

尚不支持嵌套(可嵌入)类中的Spring Data审计注释。这是the jira ticket请求此功能。

但是,我们可以使用自定义审计侦听器在可嵌入类中设置审计信息。

以下是从博客中获取的示例实现:How to audit entity modifications using the JPA @EntityListeners, @Embedded, and @Embeddable annotations

嵌入式审核

@Embeddable
public class Audit {

    @Column(name = "created_on")
    private LocalDateTime createdOn;

    @Column(name = "created_by")
    private String createdBy;

    @Column(name = "updated_on")
    private LocalDateTime updatedOn;

    @Column(name = "updated_by")
    private String updatedBy;

    //Getters and setters omitted for brevity
}

审核听众

public class AuditListener {

    @PrePersist
    public void setCreatedOn(Auditable auditable) {
        Audit audit = auditable.getAudit();

        if(audit == null) {
            audit = new Audit();
            auditable.setAudit(audit);
        }

        audit.setCreatedOn(LocalDateTime.now());
        audit.setCreatedBy(LoggedUser.get());
    }

    @PreUpdate
    public void setUpdadtedOn(Auditable auditable) {
        Audit audit = auditable.getAudit();

        audit.setUpdatedOn(LocalDateTime.now());
        audit.setUpdatedBy(LoggedUser.get());
    }
}

<强>可审计

public interface Auditable {

    Audit getAudit();

    void setAudit(Audit audit);
}

示例实体

@Entity
@EntityListeners(AuditListener.class)
public class Post implements Auditable {

    @Id
    private Long id;

    @Embedded
    private Audit audit;

    private String title;

  }

答案 1 :(得分:1)

使用 spring-data 2.4.4,AuditListener 可以很好地处理嵌入对象,请参阅 documentation spring-data

spring-boot 版本 2.4.3 中捆绑了最小版本的 spring-data