微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot + JPA - 无法删除或更新父行:外键约束失败 - 当我想添加新列表时 MYSQL

如何解决Spring Boot + JPA - 无法删除或更新父行:外键约束失败 - 当我想添加新列表时 MYSQL

当我想添加全新的列表时,出现错误

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 举报,一经查实,本站将立刻删除。