我正在开发一个带有 H2 数据库的 Spring-Boot 项目。我有两个实体Portfolio
和Report
,两者之间存在多对多关联。我希望对这些实体进行审核,因此我按照本教程通过AuditorAware
带有自定义字段的界面进行审核。
这两个实体都经过了很好的审计,列是在数据库中创建的。但是,连接表portfolio_reports
不会被审计。我怎样才能审计连接表呢?
投资组合.java
@Entity
@Table(name = "portfolio")
public class Portfolio extends Auditable<String> implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
@Unique
private String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(name = "portfolio_report", joinColumns = @JoinColumn(name = "portfolio_id"), inverseJoinColumns = @JoinColumn(name = "report_id"))
private List<Report> reports;
// Getters and setters
}
报告.java
@Entity
@Table(name = "report")
public class Report extends Auditable<String> implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "axioma_id")
private Long axiomaId;
@Column(name = "name")
private String name;
@AuditJoinTable
@ManyToMany(mappedBy = "reports", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private List<Portfolio> portfolios;
// Getters and setters
}
可审计的.java
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable<U> {
@Version
@Column(name = "version_no")
protected Long versionNo;
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_date")
protected Date createdDate;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modified_date")
protected Date modifiedDate;
}
AuditorAwareImpl.java
public class AuditorAwareImpl implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of("Admin");
}
}
持久性配置.java
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
public class PersistenceConfiguration {
@Bean
public AuditorAware<String> auditorAware() {
return new AuditorAwareImpl();
}
}