2 Replies Latest reply on Apr 27, 2012 2:09 PM by bwarren

    Latest revision for delete - Envers 1.2.2.GA

      I'm using envers 1.2.2.GA and I'm trying to find the latest revision of an entity.

       

      I have an audited entity A with a many-to-one relationship to an unaudited entity B.

       

      I want to find the latest revision number (including deletes) for the A's with a particular B .

       

      Here's my code:

       

       

          
      Number revision = (Number)AuditReaderFactory
              
        .get(entityManager).createQuery()
              
        .forRevisionsOfEntity(A.class, false, true)
              
        .addProjection(AuditEntity.revisionNumber().max())
              
        .add(AuditEntity.property("b").eq(someB))
              
        .getSingleResult();
      
      
      

       

      it generates a query like:

       

      select max(rev) from a_aud where a_b = ?;

       

      If the latest revision of an A with that B is a delete, this query misses it because that row has a null b.

       

      From what I can tell in the documentation, if setDeletedEntities = true it should take the deleted revisions into account.

       

      The query I need is something like:

       

      select max(rev) from a_aud where a_id in (select a_id from a_aud where a_b = ?);

       

      Is there something wrong with how I'm creating the query?

        • 1. Re: Latest revision for delete - Envers 1.2.2.GA
          adamw

          I think you do everything, except that you don't need to setDeletedEntites = true. All entities are always taken into account; this parameter only specifies if the "DEL" revisions (which have all-null) data should be returned. Otherwise, the last change of the entity will be returned.

           

          Unless you want to get the revision at which the entity was deleted, not the revision at which the last change was made to it. If so, you'll have to upgrade to a newer version of Hibernate and set the conf parameter store data in delted revisions to true.

           

          Adam

          1 of 1 people found this helpful
          • 2. Re: Latest revision for delete - Envers 1.2.2.GA

            I want the version number of the latest change to the set of entities, which sometimes may be a delete.

             

            Upgrading hibernate is not an option at this time so I'll just handle it with a native query.