@ Override public void addRelations ( Task task , Iterable < ObjectId > projectIds , String fieldName ) { List < Project > newProjectList = new LinkedList <> ( ) ; Iterable < Project > projectsToAdd = projectRepository . findAll ( projectIds , null ) ; for ( Project project : projectsToAdd ) { newProjectList . add ( project ) ; } try { if ( PropertyUtils . getProperty ( task , fieldName ) != null ) { Iterable < Project > projects = ( Iterable < Project > ) PropertyUtils . getProperty ( task , fieldName ) ; for ( Project project : projects ) { newProjectList . add ( project ) ; } } } catch ( Exception e ) { throw new RuntimeException ( e ) ; } try { PropertyUtils . setProperty ( task , fieldName , newProjectList ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } taskRepository . save ( task ) ; }
A simple implementation of the addRelations 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 .