1 2 Previous Next 16 Replies Latest reply on Mar 28, 2013 4:21 PM by 0x3333

    ValidityAuditStrategy with no audit record

    0x3333

      Hi!

       

      I changed to ValidityAuditStrategy but I'm having a issue.

       

      If I try to persist a new entity that have a N:1 relationship with it the ValidityAuditStrategy throw an exception:

       

      Caused by: java.lang.RuntimeException: Cannot find previous revision for entity br.com.unimaquina.erp.dynamic.PnContato_AUD and id 4
      at org.hibernate.envers.strategy.ValidityAuditStrategy.updateLastRevision(ValidityAuditStrategy.java:175)
      at org.hibernate.envers.strategy.ValidityAuditStrategy.perform(ValidityAuditStrategy.java:66)
      at org.hibernate.envers.synchronization.work.AbstractAuditWorkUnit.perform(AbstractAuditWorkUnit.java:74)
      at org.hibernate.envers.synchronization.AuditProcess.executeInSession(AuditProcess.java:114)
      at org.hibernate.envers.synchronization.AuditProcess.doBeforeTransactionCompletion(AuditProcess.java:152)
      at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)
      ... 97 more

      Caused by: java.lang.RuntimeException: Cannot find previous revision for entity br.com.ib8.snowx.Product_AUD and id 4

      at org.hibernate.envers.strategy.ValidityAuditStrategy.updateLastRevision(ValidityAuditStrategy.java:175)

      at org.hibernate.envers.strategy.ValidityAuditStrategy.perform(ValidityAuditStrategy.java:66)

      at org.hibernate.envers.synchronization.work.AbstractAuditWorkUnit.perform(AbstractAuditWorkUnit.java:74)

      at org.hibernate.envers.synchronization.AuditProcess.executeInSession(AuditProcess.java:114)

      at org.hibernate.envers.synchronization.AuditProcess.doBeforeTransactionCompletion(AuditProcess.java:152)

      at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)

      ... 97 more

       

       

      I have some records on the Product entity by there's no record on Product_AUD, because this has been created right now.

       

      The problem occurrs when the _AUD table is empty.

       

      Regards,

       

       

        • 1. Re: ValidityAuditStrategy with no audit record
          azagorneanu

          I was having the same issue. Seems that ValidityAuditStrategy tries always to update the REVEND of previous version, even if the audit table is empty. This is causing problems when you are migrating to ValidityAuditStrategy or when you start using Envers with entities which already exists (then the update for these entities will fail).

          • 2. Re: ValidityAuditStrategy with no audit record
            0x3333

            I made it work just removing the else that thrown that exception. everything is working now. I will fill a bug when JIRA get up again.

             

             

            174,177c174
            <              } else {
            <                       throw new RuntimeException("Cannot find previous revision for entity " + auditedEntityName + " and id " + id);
            <              }
            ---
            >           }
            
            • 3. Re: ValidityAuditStrategy with no audit record
              fbascheper

              Hi,

               

              There should always be exactly one row in the audit table where revend is null for any id referring to the auditedTable (except for the auditjoinTable where the situation is slightly more complicated: there should be one row with revend null for a joincolumn / inversejoincolumn combination).

              Which implies that the exception thrown in this code should be correct. However, there's probably something else going wrong.

               

              I have seen some situations where the o.h.e.entities.mapper.relation.AbstractCollectionMapper did not create new AuditJoinTable rows. This was caused by invalid equals() and hashCode() methods.

               

              Maybe you're seeing something similar. I'd be very interested in any testcase you can provide for this exception.

              • 4. Re: ValidityAuditStrategy with no audit record
                0x3333

                Erik,

                 

                I see, but sometimes a table is filled outside, from a external program for example, in that case we don't have a row in the audit table.

                • 5. Re: ValidityAuditStrategy with no audit record
                  fbascheper

                  Yes that would cause problems. External modifications to the database should create audit rows as well.

                  • 6. Re: ValidityAuditStrategy with no audit record
                    adamw

                    Just as Erik said, if a row is added outside of Hibernate/Envers, you have to manually initialize the audit table with an initial revision, type = 0 (ADD).

                     

                    Adam

                    • 7. ValidityAuditStrategy with no audit record
                      0x3333

                      FYI, I'm working on a code that will create the records in the audit table if there is none.

                       

                      Tercio

                      • 8. Re: ValidityAuditStrategy with no audit record
                        vincenzo.vitale

                        Why not letting envers working fine whitout trhowing the exception in the case the audit table for that element is not initialized?

                        • 9. Re: ValidityAuditStrategy with no audit record
                          adamw

                          Because it shouldn't happen in normal situations - the only exception is non-migrated data.

                           

                          Adam

                          • 10. Re: ValidityAuditStrategy with no audit record
                            dserodio

                            Tercio Ferdinando Gaudencio Filho wrote:

                             

                            FYI, I'm working on a code that will create the records in the audit table if there is none.

                             

                            Tercio

                            I need something similar to what you described: I'm enabling Envers in an existing database, so the tables are full of data but the audit tables are empty. Did you find a solution for this?

                            • 11. Re: ValidityAuditStrategy with no audit record
                              0x3333

                              Hi!

                               

                              Unfortunatelly, I don't have a solution yet.

                               

                              I was working in a solution directly in the database. Because if the database is big enough, creating the objects in memory would take forever.

                               

                              I did some work in MySQL creating some SPs but until now I don't have a working solution for this.

                               

                              If you can, we could work together in a solution for this. I think that it woud be helpfull for a lot devs out there.

                               

                              Tercio

                              • 12. Re: ValidityAuditStrategy with no audit record
                                dserodio

                                I'm using PostgreSQL, so a solution for MySQL databases wouldn't help me much. From what I've researched, it seems the best place to implement this solution would be AuditEventListener. On update or delete, check if the audit table already contains the history (ie the "ADD" record) for this entity. If if doesn't, insert the "ADD" record, and then the "MOD" record.

                                 

                                This way no database space would be wasted for entities that were never modified or deleted, and no manual INSERT of records would be needed.

                                 

                                My doubt is what do about the revision number, because they are "global" and not specific to the modified entity. What do you think?

                                • 13. Re: ValidityAuditStrategy with no audit record
                                  adamw

                                  Well, but that would require an additional query on each write, wouldn't it slow things down>

                                   

                                  The revision number would have to be the same as for the MOD, I guess.

                                   

                                  Adam

                                  • 14. Re: ValidityAuditStrategy with no audit record
                                    dserodio

                                    This specific application where I need to enable auditing receives few UPDATEs, and the database is a very powerful (and under-utilized) machine so I don't think performance would be a problem. Besides the possible performance issue, do you think this approach would work?

                                    1 2 Previous Next