2 Replies Latest reply on May 29, 2008 10:43 PM by medibadge

    Overriding EntityHome methods with field updates in code

    medibadge

      I have table fields that I want to update within my overridden persist and update methods. For persist() I want to set a row created date field, and for update() I want to set a row updated field. I have searched this forum for guidance and have found code (below) which works. However, I have a feeling that this approach may not be the best way for the Seam 2.0.1 GA that I am using.


      I particular, recent post http://www.seamframework.org/Community/ManuallyLoadingAnEntityHome seems to imply a cleaner method, but without a specific example (or I simply failed to understand that post).


      I want to know:


      (1) best way to override EntityHome persist() and update().


      (2) best way to access Entity fields within the persist and update methods.


      Thanks in advance.


      Dick Starr


      Here is my code:



      @Name("syLocaleHome")
      public class SyLocaleHome extends EntityHome<SyLocale> {
        
        // seam-gen code  
        public Integer getSyLocaleSyLocaleId() {
          return (Integer) getId();
        }
        public void setSyLocaleSyLocaleId(Integer id) {
          setId(id);
        }  
        
        @Override
        protected SyLocale createInstance() {
          SyLocale syLocale = new SyLocale();
          return syLocale;
        }
        
        public SyLocale getDefinedInstance() {
          return isIdDefined() ? getInstance() : null;
        }
        
        public boolean isWired() {
          return true;
        }
      
        public void wire() {
        }
      
        // my code 
        private void attachInstanceToEntityManager() {
          if (!super.isManaged() && (getInstance().getSyLocaleId() != null)) {
            setInstance(getEntityManager().merge(getInstance()));
          }
        }
        
        @Override
        public String persist() {
          attachInstanceToEntityManager();
          if (getInstance() != null) {
            Utilities utilities = new Utilities();
            Date date = utilities.getRowDateTime();
            getInstance().setRowCreated(date);
          }
          return super.persist();
        }
        
        @Override
        public String update() {
          attachInstanceToEntityManager();
          if (getInstance() != null) {
            Utilities utilities = new Utilities();
            Date date = utilities.getRowDateTime();
            getInstance().setRowUpdated(date);
          }
          return super.update();
        }
        
      
      
      }
      


        • 1. Re: Overriding EntityHome methods with field updates in code
          stephen

          I want to know:

          (1) best way to override EntityHome persist() and update().

          (2) best way to access Entity fields within the persist and update methods.

          No you don't ;-)


          You want to know how set fields in the entity with the creation and modification dates, right?


          IMHO the best way to do that is to make of of entity life cycle annotations @PrePersist and @PreUpdate.
          For example add this to your entity:


          @PrePersist
          public void prePersist() {
             rowCreated = new Date();
          }




          • 2. Re: Overriding EntityHome methods with field updates in code
            medibadge

            In other words, pick up the EJB3 In Action book that has been sitting on your desk and read it!


            I am new to both Seam and EJB3, and have been reading about Seam but ignoring the EJB3 that I am also using. Shame on me!


            This is really easy. Thanks!


            Dick