@ Override public void removeRelations ( Task task , Iterable < ObjectId > projectIds , String fieldName ) { try { if ( PropertyUtils . getProperty ( task , fieldName ) != null ) { Iterable < Project > projects = ( Iterable < Project > ) PropertyUtils . getProperty ( task , fieldName ) ; Iterator < Project > iterator = projects . iterator ( ) ; while ( iterator . hasNext ( ) ) { for ( ObjectId projectIdToRemove : projectIds ) { if ( iterator . next ( ) . getId ( ) . equals ( projectIdToRemove ) ) { iterator . remove ( ) ; break ; } } } List < Project > newProjectList = new ArrayList <> ( ) ; for ( Project project : projects ) { newProjectList . add ( project ) ; } PropertyUtils . setProperty ( task , fieldName , newProjectList ) ; taskRepository . save ( task ) ; } } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
A simple implementation of the removeRelations method which presents the general concept of the method . It SHOULD NOT be used in production because of possible race condition - production ready code should perform an atomic operation .