title: Why use Facade pattern for EJB session bean
body: <p>I want to ask what is the reason to use Facade Pattern when access EJB Session Bean. In my Netbeans 6.9.1, if I do <code>New</code> > <code>Sessions Bean for Entity Classes</code>, and let say that I select <code>User</code> entity, then Netbeans would generate this code</p>
  
  <pre><code>AbstractFacade.java
  public abstract class AbstractFacade&lt;T&gt; {
      private Class&lt;T&gt; entityClass;
  
      public AbstractFacade(Class&lt;T&gt; entityClass) {
          this.entityClass = entityClass;
      }
  
      protected abstract EntityManager getEntityManager();
  
      public void create(T entity) {
          getEntityManager().persist(entity);
      }
  
      public T edit(T entity) {
          return getEntityManager().merge(entity);
      }
  
      public void remove(T entity) {
          getEntityManager().remove(getEntityManager().merge(entity));
      }
  
      public T find(Object id) {
          return getEntityManager().find(entityClass, id);
      }
  
      public List&lt;T&gt; findAll() {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          cq.select(cq.from(entityClass));
          return getEntityManager().createQuery(cq).getResultList();
      }
  
      public List&lt;T&gt; findRange(int[] range) {
          ...
      }
  
      public int count() {
          ...
      }
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>UserFacade.java    
  
  package com.bridgeye.ejb;
  
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  @Stateless
  public class UserFacade extends AbstractFacade&lt;User&gt; {
      @PersistenceContext(unitName = "Bridgeye2-ejbPU")
      private EntityManager em;
  
      @Override
      protected EntityManager getEntityManager() {
          return em;
      }
  
      public UserFacade() {
          super(User.class);
      }        
  }
  </code></pre>
  
  <p>I want to ask what is the benefit of this. If I have 10 entities, then Netbeans would generated 10 Facade classes plus the AbstractFacade. This seems to be overkill to me. Let say somewhere inside my managed bean, i have to <code>persist</code> a <code>User</code> and <code>School</code> then I have do this</p>
  
  <pre><code>someManagedBean.java
  
  ...
  @EJB
  private UserFacade userEJB;
  
  @EJB
  private SchoolFacade schoolEJB;
  
  ...
  
  public void someMethod(User user, School school){
      ...
      userEJB.create(user);
      schoolEJB.create(school);    
  }
  </code></pre>
  
  <p><strong>Is this the right things to do?</strong></p>