Hibernate Multidirectional Multiuser Updates with L2 Cache

I have a bi-directional many-to-many class:

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

Now I save listA to B:

 b.setListA(listA); 

All this works fine until I turn on second-level caching in the a.ListB collection. Now when I update the list in B, a.listB is not updated and remains obsolete.

How do you get around this?

Thanks Dwayne

0
source share
1 answer

Are you setting up both sides of your bidirectional connection between A and B? The usual approach is to use protective methods, such as:

 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); } } 
+2
source

All Articles