title: Spring JPA save an already existing Entity of @OneToMany relationship
body: <p>I have 2 entities (I removed useless fields):</p>
  
  <pre><code>@Entity
  public class ApkPermissions {
      @Id
      @GeneratedValue
      @JsonIgnore
      private Long id;
  
      @OneToMany
      @Cascade({CascadeType.SAVE_UPDATE})
      private Collection&lt;Permission&gt; declaredPermissions;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Entity
  public class Permission {
      @Id
      @GeneratedValue
      @JsonIgnore
      private Long id;
  
      @Column(unique = true)
      private String permissionName;
  }
  </code></pre>
  
  <p>when I save the ApkPermissions entity for the first time through a CrudRepository I've no problems, but when I save another ApkPermissions with some Permission that already exists in the Permission table an exception is raised indicating the Unique Key violation of permissionName.</p>
  
  <p>I found this solution:</p>
  
  <p><a href="http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/" rel="nofollow">http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/</a></p>
  
  <p>that seems to fit my case (and is my current implementation), but it doesn't work.</p>
  
  <p>I get the following error message:</p>
  
  <pre><code>could not execute statement; SQL [n/a]; constraint [UK_l3pmqryh8vgle52647itattb9]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
  </code></pre>
  
  <p>From mysql:</p>
  
  <pre><code>UNIQUE KEY `UK_l3pmqryh8vgle52647itattb9` (`permission_name`)
  </code></pre>
  
  <hr>
  
  <p>Temporary solution is retrive all corresponding Permission searching by permissionName from a Repository. Then the save works fine.
  It's logically correct but I hope that exists a simpler procedure...</p>