title: Unidirectional OneToMany JPA persist
body: <p>I have some bizarre behavior with a unidirectional OneToMany relationship in JPA.
  This is my model:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>Entity
  @Table(name = "PRODUCTS")
  public class Product implements Serializable {
  
      @Column(name = "PRODUCT_ID_PK")
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long oid;
  
      @OneToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
      @JoinColumn(name = "PRODUCT_ID_FK", referencedColumnName = "PRODUCT_ID_PK")
      private List&lt;Attribute&gt; attributes;
  
    
      @OneToMany(orphanRemoval = true,cascade={CascadeType.ALL})
      @JoinColumn(name = "PRODUCT_ID_FK", referencedColumnName = "PRODUCT_ID_PK")
      private List&lt;Price&gt; prices;
  }
  
  @Entity
  @Table(name = "ATTRIBUTES")
  public class Attribute implements Serializable {
  
      @Column(name = "ATTRIBUTE_ID_PK")
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int oid;
  
      private String name;
  }
  
  @Entity
  @Table(name = "PRICES")
  public class Price implements Serializable {
  
      @Column(name = "PRICE_ID_PK")
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int oid;
  
      private int value;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I create a "Product" and persist a product in the db.</p>
  
  <p>Later I want to add some attributes.</p>
  
  <p>This is the code that does it:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>  //start transaction
    for (SomeElement el: someElements) {
          Attribute attr = new Attribute(el);
          product.getAttributes().addAttribute(attr);
     }
  
     for (SomeOtherElement el: someOtherElements) {
           Price price = new Price(el);
           product.getPrices().addPrice(price);
     }
     product = em.merge(product);
     em.flush();
     ...
     //end transaction</code></pre>
  </div>
  </div>
  </p>
  
  <p>The code does not report any errors, but only the first element of each collection is added. Is this the correct behavior?
  What would be the suggested way to add a whole collection of elements?</p>
  
  <p>Thanks in advance
  VR</p>