4 Replies Latest reply on Apr 20, 2007 7:19 PM by Greg Zoller

    How To Cancel Update in Home Object

    Greg Zoller Newbie

      Hello,

      If I override update() in a Home object, how can I stop the update from happening and roll back changes the user has made? Even better: Is there a way to have access to the old and new fields values?

      What I'd like to be able to do is know what the original value was so I can manually set it back in update(). Something like this:

      @Override
      public String update() {
       if( getInstance().getFavoriteColor().equals("Red") ) {
       MyHome mh = new MyHome();
       mh.setId( getInstance().getId() );
       mh.find(); // desired: mh now holds original pre-update() persisted state
       getInstance().setFavoriteColor(mh.getFavoriteColor());
       return null;
       }
       return super.update();
      }
      


      The problem is that the database lookup returns the new value not the original value I'm looking for. I've also tried using a query rather than find()--same result. This is puzzling to me! update() hasn't yet successfully completed, so why is the find() returning the new rather than the old value?

      Anybody know how I can get access to both the original and new field values inside update()?

      Thanks!
      Greg

        • 1. Re: How To Cancel Update in Home Object
          Carsten Hoehne Master

          One question:
          What do you trying to do?
          Perhaps this is your desire?
          http://www.jboss.com/index.html?module=bb&op=viewtopic&t=106710
          If not please ask again.
          And if someone knows the solution to my lasting little problem, eben better.
          Hope i could help a little.
          Ciao,
          Carsten

          • 2. Re: How To Cancel Update in Home Object
            Greg Zoller Newbie

            Carsten,

            I think we're looking at the same problem from different angles. One desired behavior I'm looking for is the ability to cancel a data entry page w/o the changes popping up in the persistent state. I have one page in my app that does this just fine and one that (like yours) somehow persists and keeps the values I wanted to throw away upon cancel. I'll have to do a line-by-line comparison to figure out what the differences are.

            The heart of my question is more about the ability to access a field's previous value prior to persisting it, for example in a Home object's update() method. Say for example in update() you'd like to take the difference between a field's old and newly set value and do something with the difference. How would you get the old value?

            At the time update() is called the Entity's field has already been set in memory via setFoo(), so no help there. Aha, I thought---I can just hit the db with a query to temporarily pull back the previous value (the find() I show in my example code). But strangely the new value comes back from the query.

            Here's what gets me: If I'm hitting the db from within the update() method the new value may exist in memory but hasn't (shouldn't) have been persisted until I call super.update(), right? So why am I getting the new value back? Hmm.

            I'd love to know what others have done if they needed to compare old/new values of fields during setting or persisting. The only other thing I thought to try next is to save old values internally in the Entity (transiently of course) and hope that old value is still the last value set when update() is called.

            Greg

            • 3. Re: How To Cancel Update in Home Object
              Pete Muir Master

              Have you looked at this? Its not quite what you are trying to do, but it does access both the old and new variables...
              http://wiki.jboss.org/wiki/Wiki.jsp?page=SeamAuditHome

              • 4. Re: How To Cancel Update in Home Object
                Greg Zoller Newbie

                Many thanks for the tip. No, this isn't quite what I'm after, but it looks quite handy for something else, so much obliged.

                I finally got the old/new thing working by simply holding a transient property that is saved off in the setter of the field I want to access old/new values for.