title: Archiving with Java/Hibernate
body: <p>I have a simple object which is mapped simply by hibernate</p>
  
  <pre><code>class SimpleObject {
     private int id;
     private String textA;
     private String textB;
     private Date date;
     private Status status;
     //+getters/setters/other stuff
  }
  </code></pre>
  
  <p>Now, since my table is gettig quite huge (a couple of millions of entries), I decided to archive all of the entries I don't really need (the user might need them later on, by checking an option they schould be able to search the two tables the actual and the archived, but that's a thing I don't carre about yet and which will be done somewhere else).<br>
  So I've decided to keep my mapping files as simple as possible and also the way of converting an object (archived/non-archived) and use simple heritage</p>
  
  <pre><code>abstract class AbstractSimpleObject {
      // idem SimpleObject
  }
  
  class SimpleObject extends AbstractSimpleObject {
  
  }
  
  class SimpleObjectArchived extends AbstractSimpleObject {
  
  }
  </code></pre>
  
  <p>I used <em>union-subclass</em> in my hibernate mapping and mapped SimpleObject to my old table, and SimpleObjectArchived to an identical table.<br>
  Upto now everything is fine, I can delete/create/update my objects.
  Now to the archiving:</p>
  
  <p>In my SimpleObjectBusinessRules class I define a method archive:</p>
  
  <pre><code>class SimpleObjectBusinessRules {
      // the daos for the SimpleObject and the SimpleObjectArchived both using HibernateDaoSupport
      SODao soDao;
      SOADao soaDao;
  
      //...
      //you can say which objects to archive by some criterias
      public void archive(Map&lt;String,Object&gt; pCrit) {
          List&lt;SimpleObject&gt; lSOs = soDao.getByCriteria(pCrit);
          //I wrote myself a converter (based on dozer)
          List&lt;SimpleObjectArchived&gt; lSOAs = Converter.convertToSOA(lSOs);
          soDao.deleteAll(lSOs);
          soaDao.saveAll(lSOAs); //based on getHibernateTemplate().saveOrUpdateAll(pEntities)
      }
  }
  </code></pre>
  
  <p>I ommited all the try/catch/...
  I get an hibernate exception on <code>saoDao.saveAll(...)</code>
  <code>org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:[...]</code></p>
  
  <p>Anyone any idea how to solve this problem? Or anyone already treated archiving with hibernate and has a better solution (or even a working solution how to archive with hibernate)?</p>