问题描述
我想删除 Point 中的点和 PointOperations,以便删除这样的实体(双向映射,OnetoMany)我需要使用这种方法:Spring Data REST + JPA remove from OneToMany collection [not owner side]
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) {
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;
}
这种方法似乎有效 - 我必须避免 ConcurrentModificationException:
if (window.getPath().getPoints() != null) {
List<Point> copiedPoints = new ArrayList<>(window.getPath().getPoints());
for (Point point : copyPoints) {
if (point.getoperations() != null && !point.getoperations().isEmpty()) {
List<PointOperation> pointOperations = new ArrayList(point.getoperations());
for (PointOperation pointOperation : pointOperations) {
point.removePointOperation(pointOperation);
}
}
window.getPath().removePoint(point);
}
}
但我的问题是这种方法是否正确?我不想删除双向映射中的嵌套实体
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)