问题描述
Cannot delete or update a parent row: a foreign key constraint fails
public class Path {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OnetoMany(mappedBy = "path",cascade = CascadeType.ALL,orphanRemoval = true)
List<Point> points;
public void addPoints(List<Point> points) {
if (!CollectionUtils.isEmpty(this.getPoints())) {
this.getPoints().clear();
}
if (this.getPoints() != null) {
this.getPoints().addAll(points);
} else {
this.setPoints(points); // the problem is only here!!!
}
}
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;
}
path.addPoints(service.createNewPoints(window));
current.setPath(pathRepository.save(path));
问题仅在于我必须设置新点(path.getPoints() == null)
:
in line path.addPoints:
} else {
this.setPoints(points); // the problem is only here!!!
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)