0

I want to remove some objects from my collection:

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);
    }

When I want to delete all points with nested operations I get NPE error:

 private void removeOldPoints(Window window) {
        if (window.getPath().getPoints() != null) {
            window.getPath().getPoints().forEach(s -> {
                if (s.getOperations() != null && !s.getOperations().isEmpty()) {
                      s.getOperations().forEach(s::removePointOperation); // NPE here!
                }
//                Optional.ofNullable(s.getOperations()).ifPresent(s::removePointOperations);
                window.getPath().removePoint(s);
            });
        }
    }

Why? Because I remove items from collection that is in use? If yes how can I solve my problem?

stack trace:

    java.util.ConcurrentModificationException: null
        at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043)
        at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997)
        at org.hibernate.collection.internal.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:883)
        at java.base/java.lang.Iterable.forEach(Iterable.java:74)
        at pl.xxx.window.service.WindowPathService.lambda$removeOldPoints$0(WindowPathService.java:64)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)

UPDATE

Unfortunately after implementing this solution with Iterator -> Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop

private void removeOldPoints(Window window) {
        if (window.getPath().getPoints() != null) {
            for (Iterator<Point> pointIterator = window.getPath().getPoints().iterator(); pointIterator.hasNext(); ) {
                Point point = pointIterator.next();
                if (point.getOperations() != null && !point.getOperations().isEmpty()) {
                    for (Iterator<PointOperation> operationIterator = point.getOperations().iterator(); operationIterator.hasNext(); ) {
                        PointOperation pointOperation = operationIterator.next();
                        point.removePointOperation(pointOperation);
                        operationIterator.remove();
                    }
                }
                window.getPath().removePoint(point);
                pointIterator.remove();
            }
        }
    } 

I have java.util.ConcurrentModificationException: null in line:

 operationIterator.remove();
Matley
  • 1,953
  • 4
  • 35
  • 73
  • Are you getting the NPE on this line? this.getOperations().remove(pointOperation); – Simon Martinelli Feb 01 '21 at 14:16
  • 1
    @Matley Please show full stack trace. – SternK Feb 01 '21 at 14:24
  • NPE is on the line below (according to the stack trace): s.getOperations().forEach(s::removePointOperation); – Matley Feb 01 '21 at 14:26
  • @SternK stack trace added – Matley Feb 01 '21 at 14:31
  • 2
    Does this answer your question? [Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) – Jens Schauder Feb 01 '21 at 14:44
  • @JensSchauder similar problem after adding iterator... Maybe I implemented this solution in the wrong way... – Matley Feb 01 '21 at 16:55
  • Uff its really difficult to remove or modify my entity Path or Path.operations - I get different errors: ConcurrentModificationException or org.springframework.dao.DataIntegrityViolationException: could not execute statement: Cannot delete or update a parent row: a foreign key constraint fails – Matley Feb 02 '21 at 00:26
  • 1
    You miss read the answer. Iterator is not the solution, it is part of the problem and in a foreach loop you are using the iterator as well. Move the delete outside the iterator or iterate over a copy so the delete doesn't affect the iterator. Other failures that appear after solving the `ConcurrentModificationException` are other problems and should be handled in separate questions. – Jens Schauder Feb 02 '21 at 07:04
  • @JensSchauder can you write your solution? I don't know how to " Move the delete outside the iterator" – Matley Feb 02 '21 at 09:43
  • 1
    You can do it like this: https://stackoverflow.com/a/223942/66686 or like this https://stackoverflow.com/a/11201224/66686 – Jens Schauder Feb 02 '21 at 09:59

0 Answers0