title: How to implements generic dao for simple CRUD operation (Java EE, EJB injections)
body: <p>I am currentlny in the process of refactoring my code and I noticed that I could use generic abstract class for CRUD operation. 
  But ufortunately I am stuck right know.
  The problem is, when I am injecting my DAO classes into Service classes I need to use remote interface of the entity(in my case Category), see the code below.</p>
  
  <p>Remote interface for generic dao</p>
  
  <pre><code>public interface IGenericDaoRemote&lt;T&gt; {
  void add(T t);
  void remove(T t);
  void update(T t);
  List&lt;T&gt; getAll(Class&lt;T&gt; type);
  }
  </code></pre>
  
  <p>Abstract class generic dao</p>
  
  <pre><code>@Stateless
  @Remote(IGenericDaoRemote.class)
  @Local(IGenericDaoLocal.class)
  public abstract class GenericDao&lt;T&gt; implements IGenericDaoLocal&lt;T&gt;, IGenericDaoRemote&lt;T&gt; {
      @PersistenceContext(name = "postgresPersistant")
      private EntityManager entityManager;
  
      @Override
      public void add(T t) {
          Session session = entityManager.unwrap(Session.class);
          session.save(t);
      }
  
      @Override
      public void remove(T t) {
          Session session = entityManager.unwrap(Session.class);
          session.delete(t);
      }
  
      @Override
      public void update(T t) {
          Session session = entityManager.unwrap(Session.class);
          session.update(t);
      }
  
      @Override
      public List&lt;T&gt; getAll(Class&lt;T&gt; type) {
          Session session = entityManager.unwrap(Session.class);
          Criteria criteria = session.createCriteria(type).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
          return criteria.list();
      }
  }
  </code></pre>
  
  <p>Example dao class:</p>
  
  <pre><code>@Stateless
  @Remote(ICategoryDaoRemote.class)
  @Local(ICategoryDaoLocal.class)
  public class CategoryDao extends GenericDao&lt;Category&gt; implements ICategoryDaoRemote, ICategoryDaoLocal {
  }
  </code></pre>
  
  <p>Service class:</p>
  
  <pre><code>@Stateless
  @Local(ICategoryServiceLocal.class)
  @Remote(ICategoryServiceRemote.class)
  public class CategoryService implements ICategoryServiceRemote, ICategoryServiceLocal {
  
      @EJB
      private ICategoryDaoRemote categoryDao; // &lt;-- here is a problem
  .
  .
  .
  }
  </code></pre>
  
  <p>As you can see, because I am using <code>ICategoryDaoRemote</code> interface as type of initialized variable I dont have access to the generic dao methods.
  Am I missing something easy/basic?
  Or is there any other way around to do this?</p>
  
  <p>Have patience for my english please, I was trying my best!</p>