2 Replies Latest reply on Aug 11, 2011 7:49 AM by bostjan242

    Auditing related entities

    bostjan242

      Hello,

       

      I have a problem with envers. I'm new at envers and I don't know how flexible envers tool is.

      I'm using envers 3.5.6

       

      I have two tables I'm trying to audit. One is table of organisations and the other is table of devices. The background for this is that we get the device(router, switch, multimedia equipment, ...) and we configure it for an organisation and then send it to them. The device can change its location many times. Data in both tables can be updated. The goal is to see where the device has been and which fields in the database have changed in both tables.

       

      So, the result of auditing should look something like this:

       

      device1:

       

      date        | organidation | action | note         |

      2011.7.22| org2            | edit     | test note1 |         device was sent to from org1a to org2         

      2011.7.14| org1a          | edit     | test note1 |         note from table of devices has been changed and we see that in this revision the

                                                                                 organisation also has a diferent name

      2011.7.12| org1            | add     | test note  |          new device has beed added

       

      I'm auditind the device table so date and note are from device_AUD table but I would also like to get name of the organisation which is in organisation_AUD.

       

      In the code my entities look something like this:

       

      device:

       

      @Entity

      @Audited

      @Table(name="wh_device")

      public class WhDeviceEntity extends WhDevice {

       

           @JoinColumn(name = "org_id", referencedColumnName = "id")

          @ManyToOne(optional = false)

          @Override

          public OrganisationEntity getOrgId(){

              Organisation result = super.getOrgId();

              if (result instanceof OrganisationEntity){

                  return (OrganisationEntity)result;

              } else {

                  return null;

              }           

          }

          public void setOrgId(OrganisationEntity orgId){

              super.setOrgId(orgId);

          }

      }

       

      organisation entity has only ist attributes and no foreign keys.

       

      The query:

       

      AuditReader reader = AuditReaderFactory.get(entityManager);

       

      AuditQuery deviceQuery = reader.createQuery().forRevisionsOfEntity(WhDeviceEntity.class, false, true)

                      .add(AuditEntity.id().eq(entity.getId())); //entity is the deviceEntity(WhDeviceEntity)

      List revDeviceList = deviceQuery.getResultList();

       

      Problem:

       

      I get all the data from device entity, but the related organisation entity, which is accessable from device entity, has all its variables set to null.

       

      Please help, I have allready wasted quite a lot of time on google trying to find a solution. Thanks


        • 1. Re: Auditing related entities
          adamw

          What should happen, is when you call device.getOrganization() you should get the organization for the device at that specific revision (so the data that you want). Obviously then something is wrong. When are you calling device.getOrganization? Maybe after the entity manager gets closed?

           

          Adam

          • 2. Re: Auditing related entities
            bostjan242

            Hello,

             

            Thanks for the reply. That is exactly what should happen when you call device.getOrganization().

            The problem "solved it self". The same problematic code was working 2 days later.

            I don't know what was wrong, but hibernate and envers were both working.

             

            Bostjan