title: One-to-many relationship: Update removed children with JPA 2.0
body: <p>I have a bidirectional one-to-many relationship.</p>
  
  <p>0 or 1 <strong>client</strong> &lt;-> List of 0 or more <strong>product orders</strong>.</p>
  
  <p>That relationship should be set or unset on both entities:
  On the client side, I want to set the List of product orders assigned to the client; the client should then be set / unset to the orders chosen automatically.
  On the product order side, I want to set the client to which the oder is assigned; that product order should then be removed from its previously assiged client's list and added to the new assigned client's list.</p>
  
  <p>I want to use pure JPA 2.0 annotations and one "merge" call to the entity manager only (with cascade options). I've tried with the following code pieces, but it doesn't work (I use EclipseLink 2.2.0 as persistence provider)</p>
  
  <pre><code>@Entity
  public class Client implements Serializable {
      @OneToMany(mappedBy = "client", cascade= CascadeType.ALL)
      private List&lt;ProductOrder&gt; orders = new ArrayList&lt;&gt;();
  
      public void setOrders(List&lt;ProductOrder&gt; orders) {
          for (ProductOrder order : this.orders) {
              order.unsetClient();
              // don't use order.setClient(null);
              // (ConcurrentModificationEx on array)
              // TODO doesn't work!
          }
          for (ProductOrder order : orders) {
              order.setClient(this);
          }
          this.orders = orders;
      }
  
      // other fields / getters / setters
  }
  
  @Entity
  public class ProductOrder implements Serializable {
      @ManyToOne(cascade= CascadeType.ALL)
      private Client client;
  
      public void setClient(Client client) {
          // remove from previous client
          if (this.client != null) {
              this.client.getOrders().remove(this);
          }
  
          this.client = client;
  
          // add to new client
          if (client != null &amp;&amp; !client.getOrders().contains(this)) {
              client.getOrders().add(this);
          }
      }
  
      public void unsetClient() {
          client = null;
      }
  
      // other fields / getters / setters
  }
  </code></pre>
  
  <p>Facade code for persisting client:</p>
  
  <pre><code>// call setters on entity by JSF frontend...
  getEntityManager().merge(client)
  </code></pre>
  
  <p>Facade code for persisting product order:</p>
  
  <pre><code>// call setters on entity by JSF frontend...
  getEntityManager().merge(productOrder)
  </code></pre>
  
  <p>When changing the client assignment on the order side, it works well: On the client side, the order gets removed from the previous client's list and is added to the new client's list (if re-assigned).</p>
  
  <p>BUT <strong>when changing on the client side, I can only add orders</strong> (on the order side, assignment to the new client is performed), but it just ignores when I remove orders from the client's list (after saving and refreshing, they are still in the list on the client side, and on the order side, they are also still assigned to the previous client.</p>
  
  <p>Just to clarify, I <strong>DO NOT want to use a "delete orphan" option</strong>: When removing an order from the list, it should not be deleted from the database, but its client assignment should be updated (that is, to null), as defined in the Client#setOrders method. How can this be archieved?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: Thanks to the help I received here, I was able to fix this problem. See my solution below:</p>
  
  <p>The client (<strong>"One" / "owned" side</strong>) stores the orders that have been modified in a temporary field.</p>
  
  <pre><code>@Entity
  public class Client implements Serializable, EntityContainer {
  
      @OneToMany(mappedBy = "client", cascade= CascadeType.ALL)
      private List&lt;ProductOrder&gt; orders = new ArrayList&lt;&gt;();
  
      @Transient
      private List&lt;ProductOrder&gt; modifiedOrders = new ArrayList&lt;&gt;();
  
      public void setOrders(List&lt;ProductOrder&gt; orders) {
      if (orders == null) {
          orders = new ArrayList&lt;&gt;();
      }
  
      modifiedOrders = new ArrayList&lt;&gt;();
      for (ProductOrder order : this.orders) {
          order.unsetClient();
          modifiedOrders.add(order);
          // don't use order.setClient(null);
          // (ConcurrentModificationEx on array)
      }
  
      for (ProductOrder order : orders) {
          order.setClient(this);
          modifiedOrders.add(order);
      }
  
      this.orders = orders;
      }
  
      @Override // defined by my EntityContainer interface
      public List getContainedEntities() {
          return modifiedOrders;
  }
  </code></pre>
  
  <p>On the <strong>facade</strong>, when persisting, it checks if there are any entities that must be persisted, too. Note that I used an interface to encapsulate this logic as my facade is actually generic.</p>
  
  <pre><code>// call setters on entity by JSF frontend...
  getEntityManager().merge(entity);
  
  if (entity instanceof EntityContainer) {
      EntityContainer entityContainer = (EntityContainer) entity;
      for (Object childEntity : entityContainer.getContainedEntities()) {
          getEntityManager().merge(childEntity);
      }
  }
  </code></pre>