4 Replies Latest reply on May 26, 2011 2:34 PM by adamw

    Envers does not audit delete for @OneToMany

    gingming

      I've an application developed under Seam with Envers as the audit solution.  It is also using Envers to support rollback feature. I've the following classes:

       

      {code}

      @Audited

      public class Facility {

           .

           .

          @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy = "facility")

          public Set<FacilityService> getFacilityServices() {

              return this.facilityServices;

          }

      }

       

      @Audited

      public class FacilityService {

          @ManyToOne(fetch = FetchType.LAZY)

          @JoinColumn(name = "serviceId", nullable = false)

          @NotNull

          public Service getService() {

              return this.service;

          }

       

          @ManyToOne(fetch = FetchType.LAZY)

          @JoinColumn(name = "facilityId", nullable = false)

          @NotNull

          public Facility getFacility() {

              return this.facility;

          }

      }

       

      @Audited

      public class Service {

          @OneToMany(fetch = FetchType.LAZY, mappedBy = "service")

          public Set<FacilityService> getFacilityServices() {

              return this.facilityServices;

          }

      }

       

      @Name("facilityHome")

      public class FacilityHome extends EntityHome<Facility> {

          @Transactional

          public String update() {

              getInstance().setModifiedDate(new Date());

              // Check if there's facility services to be removed

              facilityServiceDao.removeFacilityServices(facilityServicesToBeRemoved);
              .
              .
              // Add new facility services to the facility
              getInstance().setFacilityServices(facilityServices);
              return super.update();

          }

      }

      {code}

       

      In the FacilityHome, I've a DAO that uses native query to delete the facility services from the database.

       

      All are working fine when there are new facility services to be added the facility instance or when facility instance contains changes.  Both the FacilityService join table and Envers contains the new data and event. 

       

      However, if the only changes is removal of the facility services from the facility (even if the modified date is changed), Envers does not audit the changes. But the facility services are successfully removed from the join table.

       

      May I know if I'm doing something wrong here?  I've looked at http://community.jboss.org/thread/161528 but it does not apply to my situation.

       

      I've added a custom AuditEventListener to see if any event is fired.  It seems like onPreRemoveCollection() is being called.  However, in the parent method, collectionEntry.getLoadedPersister().isInverse() returns true and therefore onCollectionAction(event, null, collectionEntry.getSnapshot(), collectionEntry) is not being called.  I don't really know what that does anyway.  Does it cause the audit to be inserted?