6 Replies Latest reply on Mar 7, 2012 10:37 AM by ijabz

    Comparing audited record date with manually set date

    ijabz

      1>I load a file into the database and set a field called lastModified to the last Modified value of the File.

      2>Then I do various analysis on the entity which means it written back to database a few times, however it is possible that nothing has actually changed.

      3>Finally I save the entity back to the actual file, as part of this I set the lastModified value to the current date/time and save the entiry to the database.

       

      This means that if I try to load the file again I can check for it first in the database and if the lastModified value is later than the last modified date of the file I can just load the files metadata directly from the database without having to parse the file again.

       

      So far, so good.

       

      Now I add another condition to the save process I only want to save the entity back to the file (3) if there have actually been any revisions since when the file was loaded into the program, my idea was to use do AudioReader to get date of latest revision and ONLY if it is later then lastModifedDate to save changes.

       

                  AuditReader reader = AuditReaderFactory.get(session);

                  List<Number> revisions = reader.getRevisions(Song.class,song.getRecNo());

                  Number lastRevision = revisions.get(revisions.size() - 1);

                  Date lastDate = reader.getRevisionDate(lastRevision);

                 

                  if(!lastDate.after(song.getLastModified()))

                  {

                      //Dont Save

                      return;

                  }

       

      but the trouble is that when I actually do a save the date set set by Envers is always going to be later than the lastModifiedDate I set so my logic fails and I always save changes even when not neccessary.

       

      Seems like a trivial problem but cant see the solution.

        • 1. Re: Comparing audited record date with manually set date
          adamw

          I don't quite get it, why "... when I actually do a save the date set set by Envers is always going to be later than the lastModifiedDate I set ..."? If you need some special fields, maybe look at revision entities?

           

          Adam

          • 2. Re: Comparing audited record date with manually set date
            ijabz

            Because if I set timestamp to x ,and then persist the entity the audited record timestamp is going to be x + delta, where delta is the time it takes to save the entity.

             

            One idea I had was to to not audit the last save, whereby I just save the timestamp and no other data, but I don't know if it is possible to selectively audit records

            • 3. Re: Comparing audited record date with manually set date
              adamw

              I'd just add your timestamp to a custom revision entity.

               

              Adam

              • 4. Re: Comparing audited record date with manually set date
                ijabz

                I thought I has a simpler solution I added @NotAudited to lastModifiedDate

                 

                However if I retrieve the entity from the database and just modify the lastModifiedDate field and nothing else Envers still write a new audit record, the audit table doesnt contain the lastModifiedDate field but it still creates a revision, why ?

                I thought Envers was intelligent enough to realise when nothing of relevance had changed, in fact in a test case Id written I called a set method on a field but just set it to the its existing value and this didnt trigger an audit record to be created which is why I thought this.

                • 5. Re: Comparing audited record date with manually set date
                  adamw

                  It should be - that is it should only write changes if a field changed.

                   

                  Please breakpoint in EnversPostUpdateEventListenerImpl to see what's changed (ModWorkUnit.changes holds the changes).

                   

                  Adam

                  • 6. Re: Comparing audited record date with manually set date
                    ijabz

                    Hi, found the problem.

                    I retrieved my entity, but the method that retrieved the entity created and then closed a session, so although I had created a session and started a transaction in the calling method the entity was transient  even though session.update() worked for me I think that this must have confused Envers. When I passed the session to from the calling method into the entity retrieval message the update of the lastModifiedDate no longers triggers Envers to create the unneccessary audit record.