title: One To Many and Duplicate entry
body: <p>I use JPA->Hibernate. PlayFramework. I want to have relationship.  </p>
  
  <pre><code> Category - 1:n -&gt; Tag
  </code></pre>
  
  <p>Every category can have many tags, but tags do not know about it.</p>
  
  <p>So, i do like this:</p>
  
  <blockquote>
  <pre><code>@Entity
  public class Category ... {
      @OneToMany
      public List&lt;Tag&gt; tags = new LinkedList&lt;Tag&gt;();
  }
  </code></pre>
  </blockquote>
  
  <p>I have test:</p>
  
  <p>@Test
      public void playWithTags() {</p>
  
  <pre><code>    Tag tag1 = new Tag("tag1").save(); // managed by playframework
  
      Category cat1 = new Category("cat1");
      cat1.tags.add(tag1);
      cat1.save();
  
      // check if tag1 and cat1 were saved  
      assertEquals(1, Tag.count());
      assertEquals(1, Category.count());
  
      Category cat2 = new Category("cat2");
      cat2.tags.add(tag1);
      cat2.save();
  
  }
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>16:18:01,555 ERROR ~ Duplicate entry '1' for key 'tags_id'
  16:18:01,555 ERROR ~ Could not synchronize database state with session
  org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp
  ....
  java:908)
      at java.lang.Thread.run(Thread.java:619)
  Caused by: java.sql.BatchUpdateException: Duplicate entry '1' for key 'tags_id'
      at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2020)
  </code></pre>
  
  <p>It seems that cat2.save() try to do more then it should</p>
  
  <p>If if use merge() instead of save() it works good:</p>
  
  <pre><code> cat2.merge();
  </code></pre>
  
  <p>BUT WHY?</p>