Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having a single class implement every DAO interface in your entire application would be a rather bad design.</p> <p>A more typical pattern is to have a <code>BaseDAO</code> interface (also often called <code>GenericDAO</code>) and have a <code>JPABaseDAO</code>, <code>JDBCBaseDAO</code> etc. These base classes will contain methods like find/get/read, save/store/persist, update/modify and delete/remove/purge.</p> <p>Specific DAO interfaces like <code>UserDAO</code> then inherit from <code>BaseDAO</code> and concrete implementations like <code>JPAUserDAO</code> extends from <code>JPABaseDAO</code>.</p> <p>A <code>BaseDAO</code> interface could look like this:</p> <pre><code>public interface BaseDAO &lt;T&gt; { T getByID(Long ID); T save(T type); T update(T type); void delete(T type); } </code></pre> <p>And a <code>UserDAO</code> interface:</p> <pre><code>public interface UserDAO extends BaseDAO&lt;User&gt; { List&lt;User&gt; getAllAuthorized(); } </code></pre> <p>Bare bones example of a <code>JPABaseDAO</code> implementing this interface:</p> <pre><code>@Stateless public class JPABaseDAO&lt;T&gt; implements BaseDAO&lt;T&gt; { @PersistenceContext private EntityManager entityManager; private final Class&lt;T&gt; entityType; @SuppressWarnings("unchecked") public JPABaseDAO() { this.entityType = ((Class&lt;T&gt;) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]); } @Override public T getByID(Long ID) { return entityManager.find(entityType, ID); } @Override public T save(T type) { return entityManager.persist(type); } @Override public T update(T type) { return entityManager.merge(type); } @Override public void delete(T type) { entityManager.remove(entityManager.contains(type) ? type : entityManager.merge(type)); } } </code></pre> <p>And some sample <code>UserDAO</code> implementation that would inherit from it:</p> <pre><code>@Stateless public class JPAUserDAO extends JPABaseDAO&lt;User&gt; implements UserDAO { @PersistenceContext private EntityManager entityManager; @Override public List&lt;User&gt; getAllAuthorized() { return entityManager.createNamedQuery("User.getAllAuthorized", User.class) .getResultList(); } } </code></pre> <p>In practice the base class can often do some other things transparently, for instance checking if an entity implements some kind of <code>Auditable</code> interface, and automatically setting the date and user that modified it, etc. </p> <p>When using EJB to implement your DAOs, one strategy to change implementations would be to put all JDBC implementations in one package and all JPA implementations in the other. Then just include only one implementation package in your build.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload