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

Spring,Java - 如何在删除嵌套孤立实体时避免 ConcurrentModificationException

如何解决Spring,Java - 如何在删除嵌套孤立实体时避免 ConcurrentModificationException

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