title: JPA does not insert new childs from one to many relationship when we use merge
body: <p>My question is pretty similar to the following:</p>
  
  <p><a href="https://stackoverflow.com/questions/13300386/jpa-how-do-i-add-new-items-to-a-list-with-a-onetomany-annotation">JPA: How do I add new Items to a List with a OneToMany annotation</a></p>
  
  <p><a href="https://stackoverflow.com/questions/13153237/why-merging-is-not-cascaded-on-a-one-to-many-relationship">Why merging is not cascaded on a one to many relationship</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11029260/jpa-with-jta-persist-entity-and-merge-cascaded-child-entities">JPA with JTA: Persist entity and merge cascaded child entities</a></p>
  
  <p>I also had a look in <a href="https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side" rel="nofollow noreferrer">Wikibooks</a> but still not able to fix my problem</p>
  
  <p>So, my problem is that I have a Parent class with childs, and when I add a new child and use entityManager.merge(parent) to update the new children are not inserted and I get an error of null primary key.</p>
  
  <p>I'm able to create a Parent with whatever children I want, and update theses children, but not to add a new child.</p>
  
  <p>Example:</p>
  
  <pre><code>@Entity
  public class Parent {
      private String name;
      @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
      private List&lt;Child&gt; children;
  }
  @Entity
  public class Child {
      private String name;
      @ManyToOne
      private Parent parent;
  }
  </code></pre>
  
  <p>If I create a Parent with some children it works fine. If I update the children's attributes it works fine, and if I add a new child to parent it does not work.</p>
  
  <pre><code>public void foo(){
  
      Parent parent = new Parent();
      List&lt;Child&gt; children = new ArrayList&lt;Child&gt;();
      children.add(new Child());
      children.add(new Child());
      children.add(new Child());
      parent.setChildren(children);
      //it works
      entityManager.persist(parent);
  
      //and lets say that I have updated some attributes
      changeAttributesValues(parent);
      changeAttributesValues(children);
      //it still working and the values are updated properly
      entityManager.merge(parent);
  
      //but if I add some child
      List&lt;Child&gt; children = parent.getChildren();
      children.add(moreOneChild);
      parent.setChildren(children);
      entityManager.merge(parent);
      //here I got an error saying that jpa cannot insert my attribute because my PK is null
  }
  </code></pre>
  
  <p>Note: My PK is a composite Key (lets say in this example that it is the idFromParent + idCompany)</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I solved the problem removing the composite key and adding an auto generated ID. I'm posting my real code and what I have done to make it work. With this code, </p>
  
  <pre><code>@Entity
  public class Serie implements Serializable{ 
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SERIE_ID_SEQ")
      @SequenceGenerator(name = "SERIE_ID_SEQ", sequenceName = "real.serie_id_seq")
      @Column(name = "idserie")
      private Long id;
  
      @Basic
      @Column(name = "nmserie")
      private String nmSerie;
  
      @OneToMany(mappedBy = "serie", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
      private List&lt;SerieExercise&gt; serieExercises;
  
      @ManyToOne
      @JoinColumn(name = "idtrainning")
      private Trainning Trainning;
  }
  
  @Entity
  public class SerieExercise implements Serializable{
  
      @Basic
      @Column(name = "nrrepetition")
      private int nrRepetition;
  
      @Column(name = "tminterval")
      @Temporal(TemporalType.TIMESTAMP)
      private Date tmInterval;
  
      @Id
      @Basic
      @Column(name = "nrorder")
      private Integer nrOrder;
  
      @Id
      @ManyToOne
      @JoinColumn(name = "idexercise")
      private Exercise exercise;
  
      @Id
      @ManyToOne
      @JoinColumn(name = "idserie")
      private Serie serie;
  }
  </code></pre>
  
  <p>With this code I get this error when I try to insert one more SerieExercise in my List:</p>
  
  <pre><code>Caused by: org.postgresql.util.PSQLException: ERRO: ERROR: null value in column "nrorder" violates not-null constraint
    Detail: Failing row contains(null, 10, null, null, null).
      at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
      at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
      at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:366)
      at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:493)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
      ... 132 more
  </code></pre>
  
  <p>I have debugging and the values before I call entityManager.merge() are not null. I'm using hibernate provider. I'm able to insert a Serie with whatever SerieExercise I want in my list, and able to update the values, but not able to add a new SerieExercise in my List.</p>
  
  <p>To fix I made these changes:</p>
  
  <pre><code>@Entity
  public class SerieExercise implements Serializable{
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SER_EXER_ID_SEQ")
      @SequenceGenerator(name = "SER_EXER_ID_SEQ", sequenceName  = "realvida.ser_exer_id_seq")
      @Column(name = "idserieexercicio")
      private Long id;
  
      @Basic
      @Column(name = "nrrepetition")
      private int nrRepetition;
  
      @Column(name = "tminterval")
      @Temporal(TemporalType.TIMESTAMP)
      private Date tmInterval;
  
      //@Id
      @Basic
      @Column(name = "nrorder")
      private Integer nrOrder;
  
      //@Id
      @ManyToOne
      @JoinColumn(name = "idexercise")
      private Exercise exercise;
  
      //@Id
      @ManyToOne
      @JoinColumn(name = "idserie")
      private Serie serie;
  }
  </code></pre>
  
  <p>Does anyone know why it happens? Can I make this work using a composite key?</p>
  
  <p>Let me know if you need more info.</p>