6 Replies Latest reply on Mar 12, 2008 12:45 PM by mars1412

    Get previous instance in update action

    ovistanciu

      How can I obtain the previous entity values in the EntityHome.update method?


      Let's say I have a User entity, with the corresponding UserHome component. When I call the update() method in UserHome, how can I have the initial (unedited) values of the User entity (i.e. those first loaded on the page)?
      I need to do some work that needs the old values.


      Thank you.

        • 1. Re: Get previous instance in update action
          damianharvey.damianharvey.gmail.com

          I do this by creating a Clone of the entity when it is loaded into the Bean and then comparing the Clone to the instance when I do an update.


          I had to write the Clone and Compare methods in full (ie. field by field) as I couldn't find an easy method of creating a non-linked clone (so the Clone won't update when the instance updates)


          Cheers,


          Damian.

          • 2. Re: Get previous instance in update action
            ovistanciu

            Can I bother you in posting some code or more detailed info? :)


            Thank you

            • 3. Re: Get previous instance in update action
              brachie

              Hi,


              you could do this by defining a second entityManager and use this one to load the entity from the Database.


              Alexander

              • 4. Re: Get previous instance in update action
                damianharvey.damianharvey.gmail.com

                I would post my code but I think Alexander's method has just made mine redundant. I'd try that if I were you. In fact I think I'll be trying it myself.


                It would go something like this:


                In components.xml


                   <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/MyEntityManagerFactory"/>
                   <persistence:managed-persistence-context name="secondaryEntityManager" auto-create="true" persistence-unit-jndi-name="java:/MyEntityManagerFactory"/>
                


                Set your flush mode to manual. And then in your UserHome update() method:


                User cloneUser = secondaryEntityManager.find(User.class, getId());
                //now do a compare - this you will have to write manually
                String differences = UserCompare.compare(getInstance(), cloneUser);



                Cheers,


                Damian.

                • 5. Re: Get previous instance in update action
                  ovistanciu

                  Thank you guys. Got it working.


                  Beer's on me :)

                  • 6. Re: Get previous instance in update action

                    just a (newbie) question on this (hope it's just my paranoia):


                    what about concurrent changes to your object?



                    • user A opens the page - data is read from em1 and displayed

                    • while user A is still working on the object, user B also opens the same object, edits it and saves it: now it has changed in the database

                    • user A is now ready and calls your update method, which uses em2 to find the object



                    but this object now is not what you expect!
                    it already includes the changes of user b when you do the comparision. but as I understood: you expect the object to be what user A saw when she opened the page


                    note: in this case user A will get an optimistic locking exception by hibernate so she can't update the object anyway.