It's better to do it like this:

            if(!getIdentitySession().getRelationshipManager().isAssociatedByKeys(groupId, user.getUserName())) {
                 getIdentitySession().getRelationshipManager().associateUserByKeys(groupId, user.getUserName());
            }