0 Replies Latest reply on Oct 6, 2008 6:13 AM by v a

    Best practice [referencing from entity to session beans]

    v a Newbie

      I have some kind of runtime configuration in my application: entity may contain a reference to the name of some session bean.

      public interface MyPolicy
      {
       public String get();
      }
      
      @Stateless
      @Local(MyPolicy.class)
      public class MyPolicy1 implements MyPolicy
      {
       public String get()
       {
       return "This is policy 1";
       }
      }
      
      @Stateless
      @Local(MyPolicy.class)
      public class MyPolicy2 implements MyPolicy
      {
       public String get()
       {
       return "This is policy 2";
       }
      }
      
      @Entity
      ....
      public class MyEntity
      {
       private String policyClass;
       ...
      
       protected String getPolicyClass()
       {
       return policyClass;
       }
      
       protected void setPolicyClass(String policyClass)
       {
       this.policyClass = policyClass;
       }
      }
      


      Should I also add transient methods to these entities to get real session bean or use another session bean (or simple utility method) to retrieve bean at runtime?

      public class MyEntity
      {
       private String policyClass;
       private transient MyPolicy policy;
       ...
      
       protected String getPolicyClass()
       {
       return policyClass;
       }
      
       protected void setPolicyClass(String policyClass)
       {
       this.policyClass = policyClass;
       }
      
       @Transient
       public MyPolicy getPolicy()
       {
       if (policy == null)
       {
       try
       {
       policy = new InitialContext().lookup(policyClass);
       }
       catch (NamingException e)
       {
       throw new IllegalStateException("Misconfiguration exception", e);
       }
       }
      
       return policy;
       }
      }
      

      Then is some session bean:
       ...
       log.debug(myEntity.getPolicy().get());
      


      OR
       ...
       MyPolicy policy = Utils.lookup(myEntity.getPolicyClass());
       log.debug(policy.get());
      


      It's convenient to have a set of methods of entity to get these beans but I don't want these methods to be visible on RMI clients (and also I have bad (or not so bad?) dependency of persistence layer from business-logic beans interfaces).