问题描述
如果没有 ConcurrentModificationException 错误,我无法删除对象列表。我必须删除一些网状对象,因为我不想有孤儿。我使用以下解决方案:https://vladmihalcea.com/jpa-hibernate-synchronize-bidirectional-entity-associations/
我的实体:
public class Window {
@Column(nullable = false,updatable = false)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "path_id",foreignKey = @ForeignKey(name = "FK_path_w"))
private Path path;
}
然后我有:
public class Path {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OnetoMany(mappedBy = "path",cascade = CascadeType.ALL,orphanRemoval = true)
List<Point> points;
public void removePoint(Point point) {
if (point.getPath() != null && point.getPath() != this) {
throw new IllegalArgumentException(String.format("Point %s does not belong to path%s",point,this));
}
point.setPath(null);
this.getPoints().remove(point);
}
和:
public class Point{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OnetoMany(mappedBy = "point",fetch = FetchType.LAZY,orphanRemoval = true)
private List<PointOperation> operations;
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinColumn(name = "path_id",foreignKey = @ForeignKey(name = "FK_point_path"),nullable = false)
private Path path;
public void removePointOperation(PointOperation pointOperation) {
pointOperation.setPoint(null);
this.getoperations().remove(pointOperation);
}
和:
public class PointOperation {
@Column(nullable = false,updatable = false)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "point_id",foreignKey = @ForeignKey(name = "FK_point_point_operation"),nullable = false)
private Point point;
@OnetoOne(fetch = FetchType.LAZY)
@JoinColumn(name = "window_id",foreignKey = @ForeignKey(name = "FK_point_window"))
private Window window;
}
我想删除所有 PointOperations,然后删除所有 Points:
if (window.getPath().getPoints() != null) {
Iterator<Point> pointIterator = window.getPath().getPoints().iterator();
while (pointIterator.hasNext()) {
Point point = pointIterator.next();
if (point.getoperations() != null && !point.getoperations().isEmpty()) {
Iterator<PointOperation> operationIterator = point.getoperations().iterator();
while (operationIterator.hasNext()) {
PointOperation pointOperation = operationIterator.next();
point.removePointOperation(pointOperation);
operationIterator.remove();
}
}
window.getPath().removePoint(point);
pointIterator.remove();
}
}
我得到 STILL ConcurrentModificationException - 如何避免此错误并同时删除所有 PointOperations 和 Points???我真的没有找到任何解决方案!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)