/** * Sets the parameters. The specified data will be overlaid on to of any existing parameters with the same name. If a parameter with the same name doesn't * exist, a new one will exist. Any existing parameters that aren't specified in the new collection will be deleted. * * @param parameters the parameters. */ public void setParameters(Collection<JobDefinitionParameterEntity> parameters) { // Create a new holding list that contains all the parameters we want to persist. Collection<JobDefinitionParameterEntity> newParameters = new ArrayList<>(); // Loop through the new set of parameters. for (JobDefinitionParameterEntity parameter : parameters) { // Add the updated/merged parameter to the new list. newParameters.add(updateParameter(parameter)); } // If this is a new set of parameters, just use the newly created list. // Otherwise, if an existing set of parameters already exists, clear it out and add the elements of the new holding list to it. // If an existing list exists, we need to re-use it (i.e. clear and addAll) since Hibernate already has it associated with the session. // If we were to set a new list, Hibernate would throw an exception when trying to persist the new collection. if (this.parameters == null) { this.parameters = newParameters; } else { this.parameters.clear(); this.parameters.addAll(newParameters); } }