Manage collections in JPA 2.x
<p>Could you advice me what is the best way to update OneToMany relationship using JPA? Now in my application I make the following:</p>

<ol>
<li>Create new entity (Offer) and persist in db (first EJB transaction - using OfferService.update method)</li>
<li>Delete and add some objects to the entity child collection (in JSF managed bean)</li>
<li>In the second EJB transaction EntityManager.merge(entity).</li>
</ol>

<p><strong>Main problem</strong>:
- orphanRemoval not work (child orphans remain in the db).</p>

<p>Entities:</p>

<pre><code>@Entity
@Table(name = "OFFER")
public class Offer extends AbstractEntity {
    // ...
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "offer", orphanRemoval = true, fetch = FetchType.EAGER)
    private Set&lt;OfferPosition&gt; offerPositions = new HashSet&lt;OfferPosition&gt;();
    // ...
}

@Entity
@Table(name = "OFFER_POSITION")
public class OfferPosition extends AbstractEntity {    
    // ...
    @ManyToOne
    @JoinColumn(name = "OFFER_ID", nullable = false)
    private Offer offer;
    // ...
}
</code></pre>

<p>Delete OfferPosition (in JSF Managed Bean):</p>

<pre><code>public void delete(OfferPosition position) {
    this.offer.getOfferPositions().remove(position);
    position.setOffer(null);
}
</code></pre>

<p>Update Offer (in EJB):</p>

<pre><code>public class OfferService {
    @PersistenceContext
    private EntityManager em;

    public Offer update(Offer offer) {
        if (offer.getId() == null) {
            em.persist(offer);
            return offer;
        }
        return em.merge(offer);
    }    
}
</code></pre>