+ @Override
+ @Transactional(readOnly = false)
+ public MergeResult<Group> merge(Group newInstance, boolean returnTransientEntity) {
+
+ Set<GrantedAuthority> newAuthorities = newInstance.getGrantedAuthorities();
+ Map<GrantedAuthority, GrantedAuthority> mapOfAlreadyExistingAuthorities = new HashMap<GrantedAuthority, GrantedAuthority>();
+ GrantedAuthorityImpl alreadyInDB;
+ for (GrantedAuthority authority: newAuthorities){
+ if (authority instanceof GrantedAuthorityImpl){
+ alreadyInDB = grantedAuthorityDao.findAuthorityString(authority.getAuthority());
+ if (alreadyInDB != null){
+ if (alreadyInDB.getId() != ((GrantedAuthorityImpl)authority).getId()){
+ mapOfAlreadyExistingAuthorities.put(authority,alreadyInDB);
+ }
+ }
+ }
+ }
+ for (GrantedAuthority authority : mapOfAlreadyExistingAuthorities.keySet()){
+ newInstance.removeGrantedAuthority(authority);
+ newInstance.addGrantedAuthority(mapOfAlreadyExistingAuthorities.get(authority));
+ }
+
+ return dao.merge(newInstance, returnTransientEntity);
+ }