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

    Overriding EntityHome methods with field updates in code

    Dick Starr Newbie

      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();
        }
        
      
      
      }