6 Replies Latest reply on Aug 10, 2009 3:31 PM by jeanluc

    How to get the stored version of the entity

    oguzyalcin.oguzhanyalcin.gazi.edu.tr

      Hi,
      I have a form  that updates an entity and stores into db over EntityHome generated by seam. I am trying to persist the original value of the entity to another db. But when I try to access the original entity(stored in the db) via the ways listed below:




      entityManager.find(EntityName.class,getInstance().getRecordId());
      
      entityManager.createQuery("select entityName from EntityName entityName Where entityName.recordId=:id").setParameter("id",getInstance().getRecordId()).getSingleResult();



      I always get the instance of the updated entity. How can I get the values stored in the DB?
      Thx

        • 1. Re: How to get the stored version of the entity
          jeanluc

          The behaviour you mention is by design for a persistence context (PC). The possible options depend on whether you want the version in the database right now or, rather, the version I loaded in the current PC.




          • Hibernate maintains an internal snapshop of the loaded entity, but I don't know if there is a public API to access it

          • another option would be to use a Hibernate filter or a @PostLoad method to keep it the original value

          • yet another one is to use a native query (which bypasses the PC). Note that this may see a more recent version of the entity in case it was modified by another transaction.

          • maybe Hibernate Envers has something here, I haven't checked

          • or you may rethink how you do auditing (in case this was the business purpose behind the question).







          • 2. Re: How to get the stored version of the entity
            oguzyalcin.oguzhanyalcin.gazi.edu.tr

            Hi Jean,


            Thanks for your quick answer.


            I've searched for accessing the loaded entity but AFAIK no API exists for that purpose.
            @PostLoad and native query seems  the best ways for accessing it. One uses more memory at load time,  other accesses the db one more time than the other. The main reason to ask the question, really points to the 5th topic. I have to store the revisions of a record in a log db. If you  have suggestions, other than this, about the way to solve the problem I can take advice ;).Thanks again.


            • 3. Re: How to get the stored version of the entity
              joblini

              Hello Oguzhan,


              You could define another entity manager and use it to obtain the original values



              Components.xml
              <persistence:managed-persistence-context 
                  name="logEntityManager"
                  auto-create="true"
                  persistence-unit-jndi-name="java:/myEntityManagerFactory" />





              @PostUpdate
              EntityManager em = (EntityManager)Component.getInstance("logEntityManager");
              //do your query to get the original values here
              //write before/after images to db here
              



              You will also require a separate Entity Manager to write the before/after logs, because you are not allowed to perform any operations that would change the state of the entity manager during execution of a callback method.


              I would also look into Envers ...


              Regards, Ingo

              • 4. Re: How to get the stored version of the entity
                asookazian

                refer to 12.3.2 Intercepting Hibernate events and 12.3.4 Entity listeners and callbacks in JPA/Hibernate book for audit logging coverage.

                • 5. Re: How to get the stored version of the entity
                  oguzyalcin.oguzhanyalcin.gazi.edu.tr

                  Hi all,


                  I'm using a second manager to log the old values to log db(thus the logging process persists to another db) I've overrided the persist,update,remove methods entityHome classes generated by seam, made them transactional and bind them to xa-datasources. Everything works fine . The only problem was , when I try to persist the old values to log db  I can not access them from manager. The reply Jean gave solved the problem.  I'll have a look at the sections that Arbi mentioned in the response for better alternative.


                  Thanks for your help.

                  • 6. Re: How to get the stored version of the entity
                    jeanluc

                    The problem with a second EM is that it loads what is then in the database, not what you loaded. You have to take versioning into account in order to get correct auditing. It may be better to save the new version immediately to the audit table as well, instead of trying to save the previous version when a new one is created. Space is not an issue and this ensures correctness. You also don't need a separate EM.