Source:
private static void insertUserGroups() throws DAOException, DataException { List<Authorization> adminAuthorizations = new ArrayList<>(); Authorization adminAuthorization = serviceManager.getAuthorizationService().getById(1); Authorization managerAuthorization = serviceManager.getAuthorizationService().getById(2); Authorization userAuthorization = serviceManager.getAuthorizationService().getById(3); adminAuthorizations.add(adminAuthorization); adminAuthorizations.add(managerAuthorization); adminAuthorizations.add(userAuthorization); UserGroup firstUserGroup = new UserGroup(); firstUserGroup.setTitle("Admin"); firstUserGroup.setAuthorizations(adminAuthorizations); User firstUser = serviceManager.getUserService().getById(1); User secondUser = serviceManager.getUserService().getById(2); List<UserGroup> userGroups = new ArrayList<>(); userGroups.add(firstUserGroup); firstUser.setUserGroups(userGroups); secondUser.setUserGroups(userGroups); firstUserGroup.setUsers(Arrays.asList(new User[] {firstUser, secondUser })); serviceManager.getUserGroupService().save(firstUserGroup); adminAuthorization.getUserGroups().add(firstUserGroup); serviceManager.getAuthorizationService().save(adminAuthorization); UserGroup secondUserGroup = new UserGroup(); secondUserGroup.setTitle("Random"); List<Authorization> userAuthorizations = new ArrayList<>(); userAuthorizations.add(userAuthorization); secondUserGroup.setAuthorizations(userAuthorizations); serviceManager.getUserGroupService().save(secondUserGroup); UserGroup thirdUserGroup = new UserGroup(); thirdUserGroup.setTitle("Without authorizations"); serviceManager.getUserGroupService().save(thirdUserGroup); }

Target:
private static void insertUserGroups() throws DAOException, DataException { List<Authorization> adminAuthorizations = new ArrayList<>(); Authorization adminAuthorization = serviceManager.getAuthorizationService().getById(1); Authorization managerAuthorization = serviceManager.getAuthorizationService().getById(2); Authorization userAuthorization = serviceManager.getAuthorizationService().getById(3); adminAuthorizations.add(adminAuthorization); adminAuthorizations.add(managerAuthorization); adminAuthorizations.add(userAuthorization); UserGroup firstUserGroup = new UserGroup(); firstUserGroup.setTitle("Admin"); firstUserGroup.setAuthorizations(adminAuthorizations); User firstUser = serviceManager.getUserService().getById(1); User secondUser = serviceManager.getUserService().getById(2); List<UserGroup> userGroups = new ArrayList<>(); userGroups.add(firstUserGroup); firstUser.setUserGroups(userGroups); secondUser.setUserGroups(userGroups); firstUserGroup.setUsers(new ArrayList<User>(Arrays.asList(new User[] {firstUser, secondUser }))); serviceManager.getUserGroupService().save(firstUserGroup); adminAuthorization.getUserGroups().add(firstUserGroup); serviceManager.getAuthorizationService().save(adminAuthorization); UserGroup secondUserGroup = new UserGroup(); secondUserGroup.setTitle("Random"); List<Authorization> userAuthorizations = new ArrayList<>(); userAuthorizations.add(userAuthorization); secondUserGroup.setAuthorizations(userAuthorizations); serviceManager.getUserGroupService().save(secondUserGroup); UserGroup thirdUserGroup = new UserGroup(); thirdUserGroup.setTitle("Without authorizations"); serviceManager.getUserGroupService().save(thirdUserGroup); }