title: EclipseLink produces duplicate entries in one-to-many relationship
body: <p>I am using EclipseLink 2.4.1 and JPA 2.0. In my example I have the following class definitions:</p>
  
  <pre><code>@Entity
  public class Shepard {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name = "id")
      private Long id;
  
  
      @OneToMany(mappedBy = "shepard", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private List&lt;Sheep&gt; sheeps;
  
      public void addSheep(Sheep sheep) {
          if (sheep != null) {
              if (sheeps == null) {
                  sheeps = new ArrayList&lt;&gt;();
              }
  
              if (!sheeps.contains(sheep)) {
                  sheeps.add(sheep);
              }
          }
      }
  }
  
  @Entity
  public class Sheep {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name = "id")
      private Long id;
  
      @Column(name = "name")
      private String name;
  
      @ManyToOne(fetch = FetchType.EAGER, optional = false)
      @JoinColumn(name="shepard_id")
      private Shepard shepard;
  
      public Sheep(Shepard shepard, String name) {
          this.shepard = shepard;
          this.name = name;
  
          shepard.addSheep(this);
      }
  }
  </code></pre>
  
  <p>Now, when I write a Junit test and do the following. Note, that the Sheep constructor calls Shepard.addSheep(Sheep)!</p>
  
  <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("data");
  EnttyManager em = emf.createEntityManager();
  Shepard shepard = new Shepard();
  
  // I Persist Shepard
  em.getTransaction().begin();
  em.persist(shepard);
  em.getTransaction().commit();
  
  // II Persist Tilda
  Sheep tilda = new Sheep(shepard, "Tilda");
  em.getTransaction().begin();
  em.persist(tilda);
  em.getTransaction().commit();
  
  // III Persist Martha via Shepard
  Sheep martha = new Sheep(shepard, "Martha");
  em.getTransaction().begin();
  em.merge(shepard);
  em.getTransaction().commit();
  </code></pre>
  
  <p>And then do a named query for the Sheep's, I get a duplicate entry for "Tilda"!</p>
  
  <p>This does not occur, when I code</p>
  
  <pre><code>em.persist(martha);
  </code></pre>
  
  <p>as the last entity manager access, instead.</p>
  
  <p>Analyzing debug output, I can see that after <strong>II</strong> there is one Shepard object with [id=1] and one Sheep object with [id=1].
  Before and after <strong>III</strong>, Shepard holds two references of Sheeps, before with [id=<strong>1</strong>] for Tilda and [id=null] for Martha.
  After <strong>III</strong>, Shepard holds two references of Sheeps with [id=<strong>2</strong>] for Tilda and [id=3] for Martha.</p>
  
  <p>From EclipseLink debug output, I can see that <strong>III</strong> issues <strong>two</strong> insert statements into the Sheep table.
  In the database there end up <strong>three</strong> Sheep entries, with the id's as listed above.</p>
  
  <p>Further I get the following error:</p>
  
  <pre><code>[EL Warning]: 2013-06-21 09:53:26.631--UnitOfWork(221387686)--Thread(Thread[main,5,main])--Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.ValidationException
  Exception Description: The attribute [id] of class [Sheep] is mapped to a primary key column in the database. Updates are not allowed.
  </code></pre>
  
  <p>Is this a bug of EclipseLink? Or what am I doing wrong, here?</p>