I have two audited entities, A and B. Entity A holds a collection of entity B (annotated as One-to-many relationship). When inserting a new instance of A into the database, all
I solved a similar problem by adding a hidden lastUpdated date field on my equivalent of your A entity.
@Entity
public class A {
private Date lastModified;
@OneToMany(mappedBy = "a", cascade = CascadeType.ALL )
private List blist;
public void touch(){
lastModified=new Date();
}
}
Then in the related entities (like you B field), I added the following :
public class B {
@ManyToOne
private A a;
@PreUpdate
public void ensureParentUpdated(){
if(a!=null){
a.touch();
}
}
}
This ensures that a revision is added to A whenever a revision is added to B even though it requires custom code in many entities.
In your case this will ensure that A's history actually contains the revisions of B. this way you only need one query to get all the revisions for the whole A, Bs graph