How to retrieve the audited revision of relations?

后端 未结 2 1513
失恋的感觉
失恋的感觉 2020-12-20 03:54

Here is my use case

I have two entities : Personn and Email (a @OneToMany relation). Both of them are audited.

First I create a new Personn, with an Email (=

2条回答
  •  清酒与你
    2020-12-20 04:33

    I have been unable to make the custom post update listener solution work. addCollectionChangeWorkUnit doesn't seem to exist until hibernate 4.1 where it is marked private. EnversPostUpdateEventListenerImpl seems to appear at some point in hibernate 4.0

    I solved my 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();
        }
    }
    

    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.

提交回复
热议问题