Audit and @ Embedded in Spring JPA Data

I have a problem with auditing JPA for members as well @Embedded. Consider the following sample script:

I installed a test table in Oracle DB:

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

I define JPA @Entitywith audit:

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

}

Finally, I turn on JPA auditing in my @Configuration:

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

So far so good; when I create the instance AuditTest, assign it id and commit, the column is creationDatepopulated with the current timestamp as expected.

However, everything stops working when I encapsulate the audit columns in @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;
  }

}

Then I change the entity class to insert the creation date:

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

}

And unfortunately, auditing no longer works.

Does anyone know how to maintain audit functionality when using classes @Embedded?

+4
1

Update: ​​ Spring Data 2.1 M2 (Lovelace). https://jira.spring.io/browse/DATACMNS-1274

Spring () . jira, .

.

, : JPA @EntityListeners, @Embedded @Embeddable .

@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;

  }
0

All Articles