title: "state snapshots" used by Hibernate for dirty checking
body: <p>I am in reference to a book about java persistence and Hibernate.</p>
  
  <p>(See: <a href="https://www.manning.com/books/java-persistence-with-hibernate-second-edition" rel="nofollow noreferrer">https://www.manning.com/books/java-persistence-with-hibernate-second-edition</a>).</p>
  
  <p>The book repeatedly mentions "<em>state snapshots</em>" - used amongst others for dirty-checking.</p>
  
  <p>See:</p>
  
  <blockquote>
    <p>The persistence context cache never shrinks automatically. Keep the
    size of your persistence context to the necessary minimum. Often, many
    persistent instances in your context are there by accident—for
    example, because you needed only a few items but queried for many.
    Extremely large graphs can have a serious performance impact and
    require significant memory for state snapshots. Check that your
    queries return only data you need, and consider the following ways to
    control Hibernate’s caching behavior.</p>
    
    <p>You can call EntityManager#detach(i) to evict a persistent instance
    manually from the persistence context. You can call
    EntityManager#clear() to detach all persistent entity instances,
    leaving you with an empty persistence context.</p>
    
    <p>The native Session API has some extra operations you might find
    useful. You can set the entire persistence context to read-only mode.
    This disables state snapshots and dirty checking, and Hibernate won’t
    write modifications to the database:</p>
    
    <p><code>em.unwrap(Session.class).setDefaultReadOnly(true);</code></p>
    
    <p><code>Item item = em.find(Item.class, ITEM_ID);</code> </p>
    
    <p><code>item.setName("New Name");</code></p>
    
    <p><code>em.flush();</code></p>
  </blockquote>
  
  <p>My questions therefore are: </p>
  
  <ul>
  <li><p>What is the difference between the <strong>proxy objects</strong> created by Hibernate from <code>@Entity</code> classes and the <strong>state snapshots</strong> referred to above?</p></li>
  <li><p>Where can I find further documentation about those <strong>state snapshots</strong>?</p></li>
  </ul>
  
  <p><strong>edit</strong>: Taking into account Naros's answer, I found very interesting information within the hibernate documentation for the Session class: <code>Session#setDefaultReadOnly</code></p>
  
  <blockquote>
    <p>Change the default for entities and proxies loaded into this session
    from modifiable to read-only mode, or from modifiable to read-only
    mode. Read-only entities are not dirty-checked and snapshots of
    persistent state are not maintained. Read-only entities can be
    modified, but changes are not persisted. When a proxy is initialized,
    the loaded entity will have the same read-only/modifiable setting as
    the uninitialized proxy has, regardless of the session's current
    setting. To change the read-only/modifiable setting for a particular
    entity or proxy that is already in this session</p>
  </blockquote>
  
  <p>Here is the link: <a href="https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html#setDefaultReadOnly-boolean-" rel="nofollow noreferrer">https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html#setDefaultReadOnly-boolean-</a></p>