0

I want to remove points and PointOperations in Point so to remove such entities (bidirectional mapping, OneToMany) I need to use this approach: 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", cascade = CascadeType.ALL, 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;
}

This approach seems to be working - I have to avoid 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);
            }
        }

But my question is if this approach is correct? I want t remove nested entities that are in bidirectional mapping

Matley
  • 1,953
  • 4
  • 35
  • 73
  • Why not use an iterator instead of a for loop? Then you won't have a concurrent modification exception. – K.Nicholas Feb 02 '21 at 16:11
  • I'm afraid iterator doesn't wokr in my case because I invoke window.getPath().removePoint(point); where I also remove item from list. See my question here: https://stackoverflow.com/questions/65994234/spring-boot-jpa-hibernate-concurrentmodificationexception-when-deleting-en – Matley Feb 02 '21 at 18:09
  • So your code is the problem? – K.Nicholas Feb 02 '21 at 18:28
  • In this task I'm struggling with 2 issues: 1: how to remove entities Point and PointOperation (I have to set null values so that these objects can be removed within a transaction -> see methods like window.getPath().removePoint(point)) 2: how to avoid ConcurrentModificationException (I think my case is a little bit more difficult because it's not sufficient to use iterator because in methods like Path.removePoint I remove item from list so I can't invoke iterator.remove()).. Or maybe I'm wrong? – Matley Feb 02 '21 at 20:52
  • i think you should check out cascade types topic in spring boot , here is a good explanation https://www.baeldung.com/jpa-cascade-types – isa_toltar Feb 03 '21 at 11:44

0 Answers0