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?