[jboss-user] [EJB 3.0] - Best practice [referencing from entity to session beans]

hipa do-not-reply at jboss.com
Mon Oct 6 06:13:26 EDT 2008


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).

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4180524#4180524

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4180524



More information about the jboss-user mailing list