Hibernate Envers: Retrieving the right revisions of an entity with a collection property

前端 未结 3 632
醉话见心
醉话见心 2021-01-27 06:18

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

3条回答
  •  时光取名叫无心
    2021-01-27 06:27

    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

提交回复
热议问题