Source:
public void updateCertifiedProductMeasureMapping(ListingMeasure toUpdate) throws EntityRetrievalException { ListingMeasureEntity existingEntity = getEntityById(toUpdate.getId()); if (existingEntity == null) { throw new EntityRetrievalException("Could not find mapping with id " + toUpdate.getId()); } existingEntity.setMeasureId(toUpdate.getMeasure().getId()); existingEntity.setTypeId(toUpdate.getMeasurementType().getId()); existingEntity.setLastModifiedUser(AuthUtil.getAuditId()); update(existingEntity); Optional<List<CertificationCriterion>> updatedAssociatedCriteria = Optional .ofNullable(new ArrayList(toUpdate.getAssociatedCriteria())); Optional<List<CertificationCriterion>> exsitingAssociatedCriteria = Optional .ofNullable(existingEntity.getAssociatedCriteria().stream() .map(assocCriterion -> assocCriterion.convert()) .collect(Collectors.toList())); List<CertificationCriterion> removed = getRemovedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); List<CertificationCriterion> added = getAddedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); removed.stream().forEach(removedCriterion -> { Optional<ListingMeasureCriterionMapEntity> removedEntityOpt = existingEntity.getAssociatedCriteria().stream() .filter(existingCrit -> existingCrit.getCriterion().getId().equals(removedCriterion.getId())) .findAny(); if (removedEntityOpt.isPresent()) { ListingMeasureCriterionMapEntity removedEntity = removedEntityOpt.get(); removedEntity.setDeleted(true); removedEntity.setLastModifiedUser(AuthUtil.getAuditId()); update(removedEntity); } }); added.stream().forEach(addedCriterion -> { ListingMeasureCriterionMapEntity addedEntity = new ListingMeasureCriterionMapEntity(); addedEntity.setCertificationCriterionId(addedCriterion.getId()); addedEntity.setDeleted(false); addedEntity.setLastModifiedUser(AuthUtil.getAuditId()); addedEntity.setListingMeasureMapId(existingEntity.getId()); create(addedEntity); }); }

Target:
public void updateCertifiedProductMeasureMapping(ListingMeasure toUpdate) throws EntityRetrievalException { ListingMeasureEntity existingEntity = getEntityById(toUpdate.getId()); if (existingEntity == null) { throw new EntityRetrievalException("Could not find mapping with id " + toUpdate.getId()); } existingEntity.setMeasureId(toUpdate.getMeasure().getId()); existingEntity.setTypeId(toUpdate.getMeasureType().getId()); existingEntity.setLastModifiedUser(AuthUtil.getAuditId()); update(existingEntity); Optional<List<CertificationCriterion>> updatedAssociatedCriteria = Optional .ofNullable(new ArrayList(toUpdate.getAssociatedCriteria())); Optional<List<CertificationCriterion>> exsitingAssociatedCriteria = Optional .ofNullable(existingEntity.getAssociatedCriteria().stream() .map(assocCriterion -> assocCriterion.convert()) .collect(Collectors.toList())); List<CertificationCriterion> removed = getRemovedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); List<CertificationCriterion> added = getAddedAssociatedCriteria(updatedAssociatedCriteria, exsitingAssociatedCriteria); removed.stream().forEach(removedCriterion -> deleteRemovedCriterion(existingEntity, removedCriterion)); added.stream().forEach(addedCriterion -> saveAddedCriterion(existingEntity, addedCriterion)); }