2 Replies Latest reply on Jun 9, 2011 1:47 PM by wallenborn

    How to find out whether audit recorded the current transaction?

    wallenborn

      Hi

       

      i use Envers in a project (Envers is great, btw) and have the following problem: there is a dao method that saveOrUpdates a certain audited entity. My service gets this entity passed in from outside and calls saveOrUpdate. Now i would like to find out if the update part of that actually changed something in the database. I could write a diff method, retrieve the entity from the database before and after i saveOrUpdate and use the diff to tell. But i think envers and hibernate are doing this for me already, anyway. Envers only writes an entry into the REVINFO table if and only if at least one insert, update or delete was performed on my entity. Which means that the revision entity will have an ID if and only if that happened. So i'd like to this:

       

      public void storeEntity(SomeEntity e) {

        RevisionEntity re = saveEntity(e);

        if (re.getId())

          log.info("Entity e changed, changed version was stored in database");

        else

          log.info("RevisionEntity id is null");

      }

       

      @Transactional

      private RevisionEntity saveEntity(SomeEntity e) {

        dao.saveOrUpdate(entityManager, e);

        return AuditReaderFactory.get(entityManager).getCurrentRevision(RevisionEntity.class, false);

      }

       

      So far, i can see that the RevisionEntity is created, it's written to the database, too, but the log only says "RevisionEntity id is null". Is this the expected behavior, or am i just doing it wrong?

        • 1. Re: How to find out whether audit recorded the current transaction?
          adamw

          In fact the audit entries are only written when the transaction commits, not "on-the-fly"; that way Envers knows exactly what was changed (it's possible to modify an entity and revert it in one tx).

           

          But your use case is quite interesting ... you could try doing a session.flush() or entityManager.flush() and looking at the rev entity then?

           

          Adam

          • 2. Re: How to find out whether audit recorded the current transaction?
            wallenborn

            This works:

             

             

            public void storeEntity(SomeEntity e) {

              EntityManager em = emf.createEntityManager();

              em.getTransaction().begin();

              RevisionEntity re = AuditReaderFactory.get(entityManager).getCurrentRevision(RevisionEntity.class, false);

              dao.saveOrUpdate(em, e);

              em.getTransaction().commit();

              em.close();

             

              if (re.getId())

                log.info("Entity e changed, changed version was stored in database");

              else

                log.info("RevisionEntity id is null");

            }

             

            so maybe it's just my transaction management that's messed up a little.