2 Replies Latest reply on May 17, 2008 9:34 PM by cardel.ka.jelinek.seznam.cz

    How to store old property state before update

    cardel.ka.jelinek.seznam.cz

      I have some functionality that allows me to activate / deactivate user.


      I have User class


      public class User implements Serializable {
       private Boolean activated;
      
       public Boolean getActivated() {
              return activated;
          }
      
       public void setActivated(Boolean activated) {
              this.activated = activated;
          }
      }



      and home object for user (UserHome class)


      I would like to know, how can I store old (active/inactive) state of user, before somenone set it to active or inactive.


      For example:
      I am updating active user and setting it to inactive state. So I call UserHome.update() method to update this user and in this method I can read only new value of user state. I don´t know if user was before in active or in inactive state. I need to react only on changes of user state
      inactive to active and
      active to inactive.


      So I need to know in which state user was before update.


      I tried to set some help variable in UserHome class by calling help method before rendering view. This variable was setted right, but it was delete after I call update method.


      I think there exist some simple solution

        • 1. Re: How to store old property state before update
          stephen

          Is User a JPA entity?


          In that case you could do like this



          import javax.persistence.Entity;
          import javax.persistence.PostLoad;
          import javax.persistence.Transient;
          import java.io.Serializable;
          
          @Entity
          public class User implements Serializable {
              @Transient
              private Boolean activatedInitial;
              
              private Boolean activated;
          
              public Boolean getActivated() {
                  return activated;
              }
          
              public void setActivated(Boolean activated) {
                  this.activated = activated;
              }
          
              @PostLoad
              public void afterLoad() {
                  activatedInitial = activated;
              }
          }



          Not the most elegant, but as long as you need to know the initial value for only this field...

          • 2. Re: How to store old property state before update
            cardel.ka.jelinek.seznam.cz

            Yes, User is JPA Entity.


            Thank You. This is what I need.