1 Reply Latest reply on Oct 19, 2012 8:09 AM by i92jurir

    access to old data when updating with Home component

    i92jurir

      Hi all,

       

      I have to copy old data of one entity to a auditing table when someone update the entity. I have overriden the update method of the Home component of the entity.

       

      @Override
          public String update() {
              
              // get current data in db   NOT WORKING 2 WAYS
              Person old = entityManager.find(Person.class, this.instance.getId());    
              Person old = (Person) entityManager.createQuery("select p from Person p where p.id = "+ instance.getId()).getSingleResult();
              
              // create new row in auditing table   OK WORKING
              Person_Auditing person = new Person_Auditing();                               
              person.setEntityData(old.getEntityData());
              entityManager.persist(person);
              
              // Make the update of the entity in database
              return super.update();
      
      }
      

       

      My problem is when I retrieve current data from database I get data already modified. I can´t get old value to copy to auditing table. Anybody can help me?

       

      Thanks in advance.

        • 1. Re: access to old data when updating with Home component
          i92jurir

          I think the first post it´s a bit confusing.

           

          The idea was reread the original data from a row in a update operation. I was trying override the update method of the Home component to read the data from the table. Finally It´s working.

           

          @Override
              public String update() {
          
                    // save modified data from instance
                    MyEntity modifiedEntity = new Entity();
                    modifiedEntity.setAttrib1(instance.getAttrib1());
                    .......
                    .......
          
                    // recover row from database
                    MyEntity currentEntity = new Entity();
                    currentEntity.setId(modifiedEntity.getId());
                    currentEntity = entityManager.merge(currentEntity);
                    entityManager.refresh(currentEntity);
          
                    // create audit row with current data 
                    MyEntityAudit entityAudit = new MyEntityAudit(currentEntity);
                    entityManager.persist(entityAudit);
          
                    // record modified data to database
                    currentEntity.setAttrib1(modifiedEntity.getAttrib1)
                    .......
                    .......
          
                    return super.update();
          
          }
          

           

          Other better method to do the same thing? Thanks.