title: Hibernate Envers undo
body: <p>my application is working fine. But now the customer wants to undo changes.
  Already made database commits have to be undo.</p>
  
  <p>Therefore I have set up envers and enabled the two neccessary properties</p>
  
  <pre><code>    &lt;property name="org.hibernate.envers.track_entities_changed_in_revision"&gt;true&lt;/property&gt;
      &lt;property name="org.hibernate.envers.global_with_modified_flag"&gt;true&lt;/property&gt;
  </code></pre>
  
  <p>The Audit tables are created and filled automatically.
  Now my listener (CTRL+Z) executes the following snipped </p>
  
  <pre><code>public void run() {
  
      if (SystemDatabaseManager.isInitialized()) {
  
          if (AppHelper.showQuestion(Display.getCurrent().getActiveShell(), "undo?") == SWT.YES) {
  
              Session session = null;
  
              try {
  
                  session = SystemDatabaseManager.getCurrentSession();
  
                  AuditReader reader = AuditReaderFactory.get(session);
  
                  final Number revision = reader.getRevisionNumberForDate(new Date(Long.MAX_VALUE));
  
                  List&lt;Object&gt; test = reader.getCrossTypeRevisionChangesReader().findEntities(revision);
  
                  session.beginTransaction();
                  for(Object object: test) {
  
                      session.merge(object);
  
                  }
                  SystemDatabaseManager.commit(session);
  
              } catch (Exception e) {
                  SystemDatabaseManager.rollback(session);
              }
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Now the problem is merging the old object state throws an Exception</p>
  
  <p>"Deleted object whould be resaved ..."</p>
  
  <p>I know this Exception and what it stands for but I do not know how to solve it in this way.</p>
  
  <p>The big problem is that not only primitive fields are changing. Also relations are changed.</p>
  
  <p>Is there any solution for my problem?</p>