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();
}
}
Problem:
Clearly here
Auditable
should add some column to your intermediate table that maintains relation betweenPortfolio
andReport
and that table is created behind the scene and you don't have access to that table in your program. Only hibernate can use that table to maintain relation between your entities and do join operation.Solution:
Here you should make Join table that maintain Many to Many relation between
Portfolio
andReport
explicit so that you can have entity likePortfolioReport
in your program that can extends fromAuditable
. Please read the following post to see how to do that: The best way to map a many-to-many association with extra columns when using JPA and Hibernate