title: How to revert changes to collection owned by entity in Hibernate?
body: <p>This should be a trivially easy task, but I can't get it to work.</p>
  
  <p>I want to be able to revert changes to a lazy-loaded collection (let's call it kittens) hanging off the main entity (let's call it Cat) which are maintained in a single Hibernate session.</p>
  
  <p>The collection is defined as so (replacing my dull entities with cats and kittens):</p>
  
  <pre><code>@Entity
  @Table(name="CAT")
  public class Cat {
  
      private Map&lt;Long, Kitten&gt; kittens;
  
      @OneToMany(fetch=FetchType.LAZY, mappedBy="mother", cascade={CascadeType.ALL})
      @MapKey(name="id")
      public Map&lt;Long, Kitten&gt; getKittens() {
          return kittens;
      }
  
      ...
  }
  
  @Entity
  @Table(name="KITTEN")
  public class Kitten {
  
     private Cat mother;
  
      @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CadcadeType.MERGE)
      @JoinColumn(name="MOTHER_CAT_ID", nullable=false)
      public Cat getMother {
          return mother;
      }
  
      ...
  }
  </code></pre>
  
  <p>Users are able add to and remove from the kittens collection to their heart's content without it being persisted until they explicitly save and a session flush is performed.</p>
  
  <p>But if the user chooses to revert the original list of kittens should be retrieved again from the database.</p>
  
  <p>I have tried to achieve this full reversion of the collection by evicting the kittens collection from the second-level cache and then re-getting the Cat entity from the database using the following pattern:</p>
  
  <pre><code>public class CatDao {
  
      public Cat reset(Cat cat) {
  
          SessionFactory sessionFactory = Util.getSessionFactory();
          sessionFactory.evictCollection(Cat.class.getName() + ".kittens", cat.getId());
  
          Cat original = (Cat)this.session.get(Cat.class, cat.getId());
          Hibernate.initialize(original.getKittens());
  
          return original;
      }
  
      ...
  }
  </code></pre>
  
  <p>It doesn't re-retrieve the kittens from the database (as shown by logging). What am I doing wrong?</p>