title: Hibernate generic DAO, Generic Service, and Generic View Layer?
body: <p>I have been trying to fully understand and implement a GenericDAO layer in hibernate. I am new to the concept and have been doing a bit of reading and studying. I have found numberous examples on example implementations of a GenericDAO Layer, and this is what I have ended up with. </p>
  
  <pre><code>public class GenericDAOImpl&lt;T, ID extends Serializable&gt; implements GenericDAO&lt;T, ID&gt; {
  
      private static Logger log = Logger.getLogger(GenericDAOImpl.class.getName());
  
  
      private SessionFactory sessionFactory;
  
  
      @SuppressWarnings("unchecked")
      public T findById(long id, Class&lt;T&gt; objectClass) {
          log.info("Entered GenericDAOImpl findById(" + id +")");
          T result = (T) getSessionFactory().getCurrentSession().load(objectClass, id);
          if(result != null){
              Hibernate.initialize(result);
              return result;
          }else{ 
              return null;
          }
      }
  
      public boolean create(T newInstance) {
          log.info("Entered GenericDAOImpl create()");
          if(newInstance == null){
              return false;
          }
          getSessionFactory().getCurrentSession().saveOrUpdate(newInstance);
          return true;        
      }
  
  
      public boolean updpate(T updateInstance) {
          log.info("Entered GenericDAOImpl updpate()");
          if(updateInstance == null){
              return false;
          }
          getSessionFactory().getCurrentSession().update(updateInstance); 
          return true;
      }
  
      public boolean delete(T entity) {
          log.info("Entered GenericDAOImpl delete()");
          if(entity == null){
              return false;
          }
          getSessionFactory().getCurrentSession().delete(entity);
          return true;
      }
  
      @SuppressWarnings("unchecked")
      public List&lt;T&gt; findByExample(T exampleInstance, Class&lt;T&gt; objectClass){
          log.info("Entered GenericDAOImpl findByExample()");
          Criteria searchCriteria = getSessionFactory().getCurrentSession().createCriteria(objectClass);
  
          searchCriteria.add(Example.create(exampleInstance));
  
          return (List&lt;T&gt;)searchCriteria.list();          
  
      }
  
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }       
  }
  </code></pre>
  
  <p>It seems as though this should work in theory (may need some tweaking)</p>
  
  <p>My question is can I use a generic <code>service</code> and <code>view layer</code> to "pass through" the layered architecture approach? I do not fully understand hibernate transactions enough to know if it is safe to do this, with its handling of transactions etc... </p>
  
  <p>Maybe something like this for the service layer for example</p>
  
  <pre><code>public class GenericServiceImpl&lt;T, ID extends Serializable&gt; implements GenericService&lt;T, ID&gt;{
  
      private GenericDAO&lt;T, ID&gt; genericDao;
  
      @Override
      public T findById(long id, Class&lt;T&gt; objectClass) {
          return this.getGenericDao().findById(id, objectClass);
      }
  
      @Override
      public boolean create(T newInstance) {
          return this.getGenericDao().create(newInstance);
      }
  
      @Override
      public boolean updpate(T updateInstance) {
          return this.getGenericDao().updpate(updateInstance);
      }
  
      @Override
      public boolean delete(T entity) {
          return this.getGenericDao().delete(entity);
      }
  
      @SuppressWarnings({ "unchecked", "rawtypes" })
      @Override
      public List findByExample(T exampleInstance, Class&lt;T&gt; objectClass) {
          return this.getGenericDao().findByExample(exampleInstance, objectClass);
      }
  
  
      public GenericDAO&lt;T, ID&gt; getGenericDao() {
          return genericDao;
      }
  
      public void setGenericDao(GenericDAO&lt;T, ID&gt; genericDao) {
          this.genericDao = genericDao;
      }
  
  
  }
  </code></pre>
  
  <p>Then could I go on and do a generic view layer as well?</p>
  
  <p>Please let me know if this approach is acceptable or if there are any concerns with this approach.</p>
  
  <p>Thanks in advance for your thoughts and responses!</p>