1 Reply Latest reply on Jan 13, 2012 7:14 AM by Adam Warski

    entity groups auditing / entityChanged invoked twice on a 1-5 elements to collection add

    Vyacheslav Sakhno Newbie

      So i'm currently solving entity search by entity group issue discussed here https://community.jboss.org/message/642702#642702

       

      I'm adding from 1 to 5 elements to collection:

       

      @Entity

      @Table(name = "CONTRACTS")

      @Audited

      public class Contract implements Auditable {

          @Id

          @GeneratedValue

          @Column(name = "ID")

          private Long id;

       

      ...

      @ManyToMany(fetch = FetchType.LAZY)

          @JoinTable(name = "CONTRACT_SERVICES", joinColumns = {@JoinColumn(name = "CONTRACT_ID")}, inverseJoinColumns = {@JoinColumn(name = "SERVICE_ID")})

          @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)

          private List<ContractAddService> addServices = new ArrayList<ContractAddService>(0); // this collection

      ...

      i'm listening to entityChanged event:

       

      @Service

      public class CustomRevisionListener implements EntityTrackingRevisionListener, ApplicationContextAware {

       

      ...

                @Override

                public void entityChanged(@SuppressWarnings("rawtypes") Class entityClass, String entityName, Serializable entityId, RevisionType revisionType, Object revisionEntity) {

                          try {

                                    Auditable auditable = getEntityRevisionService().loadLastAvailableObjectState(entityClass, entityId);

                                    auditable.getAuditGroupName();

                                    auditable.getAuditGroupId();

                                    Revision revision = (Revision) revisionEntity;

      //create entity change here.

                                    EntityChange entityChange = getEntityChangeService().createDetached(revision, revisionType, entityName, entityId, auditable);

                                    getEntityChangeService().save(entityChange);

                                    revision.getEntitiesChanges().add(entityChange);

                                    getRevisionService().save(revision);

                          } catch (Exception e) {

                                    logger.error(e.getMessage());

                                    e.printStackTrace();

                          }

                }

      }

       

      The thing is listener is invoked twice with the same parameters for collection change, no matter adding 1 or 5 elements to collection:

       

      and as a result i have dublicate entries in ENTITY_CHANGED_IN_REVISION table:

       

      ID,ENTITY_ID,ENTITY_NAME,GROUP_ENTITY_ID,GROUP_NAME,REVISION_TYPE,REV_ID

      8246,1,ru.csbi.registry.domain.contract.Contract,1,Contract,1,263

      8245,1,ru.csbi.registry.domain.contract.Contract,1,Contract,1,263

       

       

       

       

      Hibernate: insert into CONTRACT_SERVICES (CONTRACT_ID, SERVICE_ID) values (?, ?)

      Hibernate: insert into CONTRACT_SERVICES (CONTRACT_ID, SERVICE_ID) values (?, ?)

      Hibernate: insert into CONTRACT_SERVICES (CONTRACT_ID, SERVICE_ID) values (?, ?)

      Hibernate: insert into CONTRACT_SERVICES (CONTRACT_ID, SERVICE_ID) values (?, ?)

      Hibernate: insert into CONTRACT_SERVICES (CONTRACT_ID, SERVICE_ID) values (?, ?)

      Hibernate: insert into CONTRACT_SERVICES (CONTRACT_ID, SERVICE_ID) values (?, ?)

      Hibernate: select REVISIONS_SEQ.nextval from dual

      Hibernate: select ENTITY_CHANGED_IN_REVISION_SEQ.nextval from dual

      Hibernate: select ENTITY_CHANGED_IN_REVISION_SEQ.nextval from dual

      Hibernate: insert into REVISIONS (TIMESTAMP, USERNAME, id) values (?, ?, ?)

      Hibernate: insert into CONTRACT_SERVICES_AUD (REVTYPE, REV, CONTRACT_ID, SERVICE_ID) values (?, ?, ?, ?)

      Hibernate: insert into CONTRACT_SERVICES_AUD (REVTYPE, REV, CONTRACT_ID, SERVICE_ID) values (?, ?, ?, ?)

      Hibernate: insert into CONTRACT_SERVICES_AUD (REVTYPE, REV, CONTRACT_ID, SERVICE_ID) values (?, ?, ?, ?)

      Hibernate: insert into CONTRACT_SERVICES_AUD (REVTYPE, REV, CONTRACT_ID, SERVICE_ID) values (?, ?, ?, ?)

      Hibernate: insert into CONTRACT_SERVICES_AUD (REVTYPE, REV, CONTRACT_ID, SERVICE_ID) values (?, ?, ?, ?)

      Hibernate: insert into CONTRACT_SERVICES_AUD (REVTYPE, REV, CONTRACT_ID, SERVICE_ID) values (?, ?, ?, ?)

      Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)

      Hibernate: insert into CONTRACTS_AUD (REVTYPE, AREA, CLOSED, DESCRIPTION, CREATED, CURRENCY_RATE, FIXED_CURRENCY_RATE, GUARANTEE_PAYMENT, GUARANTEE_VARY, GUARANTEE_VARY_COMMENT, ID_OLD, INCREASE, INCREASE_DATE, INCREASE_SUM, IS_INDIFINITELY, INITIAL_SUM, NAME, NUM, PAYMENT_FROM, PAYMENT_TO, PAYMENT_UNLIMITED, SUM, WORKFLOW_NUMBER, CURRENCY_ID, FUND_ID, INCREASE_KIND_ID, INCREASE_TYPE_ID, AREA_TYPE_ID, PAYMENT_MONTH_ID, PAYMENT_NEXT_MONTH_ID, PAYMENT_PERIOD_ID, PAYMENT_REASON_ID, PERIODICITY_ID, PROPERTY_ID, STATUS_ID, TYPE_ID, ID, REV) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

      Hibernate: insert into ENTITY_CHANGED_IN_REVISION (ENTITY_ID, ENTITY_NAME, GROUP_ENTITY_ID, GROUP_NAME, REV_ID, REVISION_TYPE, ID) values (?, ?, ?, ?, ?, ?, ?)

       

      All changes in CONTRACTS_AUD have the same data, so only @OneToMany relation is edited and is listened twice by entityChange().

       

      CONTRACTS_AUD :

       

      ID,REV,REVTYPE,AREA,CLOSED,DESCRIPTION,CREATED,CURRENCY_RATE,FIXED_CURRENCY_RATE,GUARANTEE_PAYMENT,GUARANTEE_VARY,GUARANTEE_VARY_COMMENT,ID_OLD,INCREASE,INCREASE_DATE,INCREASE_SUM,IS_INDIFINITELY,INITIAL_SUM,NAME,NUM,PAYMENT_FROM,PAYMENT_TO,PAYMENT_UNLIMITED,SUM,WORKFLOW_NUMBER,CURRENCY_ID,FUND_ID,INCREASE_KIND_ID,INCREASE_TYPE_ID,AREA_TYPE_ID,PAYMENT_MONTH_ID,PAYMENT_NEXT_MONTH_ID,PAYMENT_PERIOD_ID,PAYMENT_REASON_ID,PERIODICITY_ID,PROPERTY_ID,STATUS_ID,TYPE_ID

      1,263,1,,,,08.12.2010 1:00:00,000000,1,0,,0,,,0,,,0,0,Договор аренды офиса,123,,,0,0,23 DFG,,,,,,,,,,,17,1,1

      1,261,1,,,,08.12.2010 1:00:00,000000,1,0,,0,,,0,,,0,0,Договор аренды офиса,123,,,0,0,23 DFG,,,,,,,,,,,17,1,1

      1,262,1,,,,08.12.2010 1:00:00,000000,1,0,,0,,,0,,,0,0,Договор аренды офиса,123,,,0,0,23 DFG,,,,,,,,,,,17,1,1

      1,264,1,,,,08.12.2010 1:00:00,000000,1,0,,0,,,0,,,0,0,Договор аренды офиса,123,,,0,0,23 DFG,,,,,,,,,,,17,1,1