I'm working on a Spring-Boot project with a H2 database. I have two entities Portfolio
and Report
, and there is a many-to-many association between the two.
I want those entities to be audited, so I followed this tutorial to audit through an AuditorAware
interface with custom fields.
The two entities are well audited, the columns are created in the database. However, the join table portfolio_reports
is not audited. How can I audit the join table as well ?
Portfolio.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
}
Report.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
}
Auditable.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");
}
}
PersistenceConfiguration.java
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
public class PersistenceConfiguration {
@Bean
public AuditorAware<String> auditorAware() {
return new AuditorAwareImpl();
}
}