title: Hibernate: lazy collections and session.merge
body: <p><em><strong>What I have:</em></strong></p>
  
  <p>I need to update my <code>entity</code>. I use Hibernatee conversation, that means that we already have <code>entity</code> in the session cache.</p>
  
  <pre><code>public void handle(Request request, Session session) {
    MyEntity updatedEntity = request.getEntity();
    session.merge(updatedEntity); //rewrite all lazy collections
  }
  </code></pre>
  
  <p>So I send object to client, client full up the object and send it back for update.</p>
  
  <p><em><strong>What the problem:</em></strong></p>
  
  <p><strong>Lazy collections aren't sent to the client. As a result, if lazy collection hasn't been empty, than it will be overriden in <code>session.merge(updatedEntity)</code> string</strong></p>
  
  <p><em>It happens because client knows nothing about element in those collections</em></p>
  
  <p><em><strong>Question:</em></strong></p>
  
  <p>How two merge <code>entity</code> in correct way? Means without rewriting lazy collections.</p>
  
  <p><em><strong>EDIT:(how I work with my collections)</em></strong></p>
  
  <pre><code>public class MyEntity {
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "entity_id")
    private List&lt;AnotherEntity&gt; anotherEntities;
  
  
    public void setAnotherEntities(List&lt;AnotherEntity&gt; anotherEntities) {
      // we must work with the same instance of list (again, because of orphanRemoval)
      this.anotherEntities.clear();
      this.anotherEntities.addAll(anotherEntities);
    }
  }
  </code></pre>