title: Avoid ConstraintViolationException in JPA and OneToMany relationship when merge
body: <p>What I want to achieve with the example below is to avoid the constraint violation exception that occurs
  when run the following:</p>
  
  <pre><code>Parent p = new Parent();
  Set&lt;Child&gt; children = new HashSet&lt;Child&gt;();
  Child c = new Child();
  children.add(c);
  p.setChildren(children);
  
  entityManager.merge(p);
  entityManager.merge(p);
  </code></pre>
  
  <p>How to configure JPA to achieve this effect? Below are the classes.</p>
  
  <pre><code>@Entity
  @Table(name = "parent", indexes = {uniqueConstraints = { @UniqueConstraint(columnNames = { "uri" }) })
  public class Parent implements Serializable {
    ...
    private String uri;
    private Set&lt;Child&gt; children = new HashSet&lt;Child&gt;();
  
  
    @Id
    @Column(name = "id", unique = true)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parent_seq")
    @SequenceGenerator(name = "parent_seq", sequenceName = "parent_seq", allocationSize = 1)
    public Long getId() {
      return id;
    }
  
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "parent_id")
    public Set&lt;Child&gt; getChildren() {
      return children;
    }
  
  @Override
    public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Parent other = (Parent) obj;
          if (uri == null) {
              if (other.uri != null)
                  return false;
          } else if (!uri.equals(other.uri))
              return false;
          return true;
      }
  
  
  @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((uri == null) ? 0 : uri.hashCode());
          return result;
      }
  
  
  
  }
  
  @Entity
  @Table(name = "child", indexes = {uniqueConstraints = { @UniqueConstraint(columnNames = { "text" }) })
  public class Child implements Serializable {
  
    private String text;
  
    @Id
    @Column(name = "id", unique = true)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "child_seq")
    @SequenceGenerator(name = "child_seq", sequenceName = "child_seq", allocationSize = 1)
    public Long getId() {
      return id;
    }
  
  
  
    @Override
    public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          Child other = (Child) obj;
          if (text == null) {
              if (other.text != null)
                  return false;
          } else if (!text.equals(other.text))
              return false;
          return true;
      }
  
  
  @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((text == null) ? 0 : text.hashCode());
          return result;
      }
  }
  </code></pre>
  
  <p>I use postgres and hibernate as JPA provider</p>