使用二级缓存休眠双向ManyToMany更新 [英] Hibernate Bi-Directional ManyToMany Updates with Second Level cache
问题描述
我有一个双向多对多课程:
pre $ public class A {
@ManyToMany(mappedBy =listA)
私人列表< B>数组listB;
}
public class B {
@ManyToMany
private List< A> listA的;
}
现在我将listA保存到B中:
b.setListA(listA);
这一切都正常工作,直到我打开集合a.ListB的二级缓存。现在,当我更新B中的列表时,a.listB不会更新并且保持陈旧。
您如何解决此问题?
感谢,
Dwayne
您是否设置了双向链接A和B正确吗?一个典型的方法是使用这样的防御性方法:
public class B {
@ManyToMany
private列表与LT 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);
}
}
I have a bidirectional 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 a listA into B:
b.setListA(listA);
This all works fine until I turn on second-level caching on the collection a.ListB. Now, when I update the list in B, a.listB does not get updated and remains stale.
How do you get around this?
Thanks, Dwayne
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);
}
}
这篇关于使用二级缓存休眠双向ManyToMany更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!