title: Spring Data: How to update a List of childs with unique constraints
body: <p>I am using spring-data to persist data to a mysql db.
  I have a parent entity that contains a list of child entitys:</p>
  
  <pre><code>@Entity
  public class Parent
  {
      private Long id;
  
      @Unique
      private String name;
  
      @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
      @JoinColumn(name = "parent_id")
      private List&lt;Child&gt; childs;
  
      // Getter, Setter
  }
  </code></pre>
  
  <p>and here is the child entity:</p>
  
  <pre><code>@Entity
  @Table(
      uniqueConstraints = { @UniqueConstraint(columnNames = { "parent_id", "name" }) }
  )
  public class Child
  {
      private Long id;
  
      @Unique
      private String name;
  
      @ManyToOne(cascade = CascadeType.ALL)
      @JoinColumn(name = "parent_id")
      private Parent parent;
  
      // Getter, Setter
  }
  </code></pre>
  
  <p>In the database I have the same constraints active like in this two classes.
  Now, I want to update the childs of the parent like this:</p>
  
  <pre><code>@Service
  public class ParentService
  {
      // ...
  
      @Transactional
      public Parent updateParent(String name, List&lt;Child&gt; newChilds)
      {
          Parent existingParent = parentRepository.findByName(name);
          existingParent.setChilds(newChilds);
          return parentRepository.save(existingParent);
      }
  
      // ...
  }
  </code></pre>
  
  <p>But then the unique constraint (name + parent_id) for the child will fail, if they have the same name. How can I get sure, that the old childs are deleted before the new childs are persisted?</p>