A {
@OneToMany(cascade = CascadeType.ALL)
List<B> listB;
}
Suppose A has a list of B, if I update A using A.setListB(anotherList)
, items in the original listB are still in the database, what should I do to automatically delete items B which are not in the new listB when I update A?
尝试在注释上使用orphanRemoval=true
With JPA, you typically do not reassign list/collection variables, as this leads to errors. This is due to the JPA provider "tracking" the collections when you load an object from the database. Rather, you should do this:
listB.clear();
listB.addAll(otherList);
Combined with orphanRemoval=true
as suggested by the other answer, it should behave the way that you wish it to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.