Hibernate Bi-Directional ManyToMany Updates with Second Level cache

前端 未结 1 486
天涯浪人
天涯浪人 2021-01-27 10:20

I have a bidirectional many-to-many class:

public class A{
 @ManyToMany(mappedBy=\"listA\")
 private List listB;
}
public class B{
 @ManyToMany
 private         


        
相关标签:
1条回答
  • 2021-01-27 11:15

    Are you setting both sides of your bidirectional link between A and B correctly? A typically approach is to use defensive methods like this:

    public class B {
        @ManyToMany
        private List<A> listA;
    
        public void addToListA(A a) {
            this.listA.add(a);
            a.getListB().add(this);
        }
    
        public void removeFromListA(A a) {
            this.listA.remove(a);
            a.getListB().remove(this);
        }
    }
    
    0 讨论(0)
提交回复
热议问题