Removing Collection Data Container

I need to remove the Collection (composition) from the data container and have the following code:

 private void removeOldPaymentSchedule(){
        if(getEditedEntity().getPropertyAllotmentProposedPaymentSchedule()!=null) {

            List<PropertyAllotmentProposedPaymentSchedule> toRemove = new ArrayList<>();
            
            Iterator<PropertyAllotmentProposedPaymentSchedule> iteratorLineList = propertyAllotmentProposedPaymentScheduleDc.getItems().iterator();
            while (iteratorLineList.hasNext()) {
                PropertyAllotmentProposedPaymentSchedule line = iteratorLineList.next();
                toRemove.add(line);
            }

           for (PropertyAllotmentProposedPaymentSchedule schedule : toRemove) {
                propertyAllotmentProposedPaymentScheduleDc.getMutableItems().remove(schedule);
               
            }
        }
    } 

When I run it, the collection data displayed in a screen (in a table) is removed but after I save it and reopen, see it was not deleted.

Only when I use the following 2nd line of code then it works.

propertyAllotmentProposedPaymentScheduleDc.getMutableItems().remove(schedule); 
dataContext.remove(schedule); 

Is it the right approach?

Yes, that’s correct.

1 Like