title: Assistance with generic service/dao layer
body: <p>I want to write generic service and DAO layer.</p>
  
  <p>Here is UML of used entities. <img src="https://i.stack.imgur.com/9ZYWR.jpg" alt="enter image description here"></p>
  
  <p><em>1. AbstractDAO</em></p>
  
  <pre><code>public interface AbstractDAO&lt;T&gt; {
  
      public List&lt;T&gt; getAll();
  
  }
  </code></pre>
  
  <p><em>2. AbstractDAOImpl</em></p>
  
  <pre><code>@Repository
  public abstract class AbstractDAOImpl&lt;T&gt; implements AbstractDAO&lt;T&gt; {
  
      private final Class&lt;T&gt; clazz = (Class&lt;T&gt;) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  
      @Autowired
      private SessionFactory sessionFactory;
  
      @Override
      public List&lt;T&gt; getAll() {
          String query = "from " + clazz.getSimpleName().toString();
          return sessionFactory.getCurrentSession().createQuery(query)
                  .list();
      }
  
  }
  </code></pre>
  
  <p><em>3. PaymentTypeDAO</em></p>
  
  <pre><code>public interface PaymentTypeDAO extends AbstractDAO&lt;PaymentType&gt; {
  
  }
  </code></pre>
  
  <p><em>4. PaymentTypeDAOImpl</em></p>
  
  <pre><code>@Repository
  public class PaymentTypeDAOImpl extends AbstractDAOImpl&lt;PaymentType&gt; implements PaymentTypeDAO {
  
      @Autowired
      private SessionFactory sessionFactory;
  
  }
  </code></pre>
  
  <p><em>5. AbstractService</em></p>
  
  <pre><code>@Transactional
  public interface AbstractService&lt;T&gt; {
  
      @Transactional
      public List&lt;T&gt; getAll();
  
  }
  </code></pre>
  
  <p><em>6. AbstractServiceImpl</em></p>
  
  <pre><code>@Service
  @Transactional
  public class AbstractServiceImpl&lt;T&gt; implements AbstractService&lt;T&gt; {
  
  
      @Autowired
      private AbstractDAO&lt;T&gt; dao;
  
      @Transactional
      public List&lt;T&gt; getAll() {
          return dao.getAll();
      }
  
      protected AbstractDAO&lt;T&gt; getDao() {
          return dao;
      }
  
      protected void setDao(AbstractDAO&lt;T&gt; dao) {
          this.dao = dao;
      }
  
  }
  </code></pre>
  
  <p><em>7. PaymentTypeService</em></p>
  
  <pre><code>@Transactional
  public interface PaymentTypeService extends AbstractService&lt;PaymentType&gt; {
  
  }
  </code></pre>
  
  <p><em>8. PaymentTypeServiceImpl</em></p>
  
  <pre><code>@Service
  @Transactional
  public class PaymentTypeServiceImpl extends AbstractServiceImpl&lt;PaymentType&gt; implements PaymentTypeService {
  
      @Autowired
      private PaymentTypeDAO paymentTypeDAO;
  
      public PaymentTypeServiceImpl() {
          super.setDao(paymentTypeDAO);
      }
  
  //  @Override
  //  public List&lt;PaymentType&gt; getAll() {
  //      return paymentTypeDAO.getAll();
  //  }
  //  
  }
  </code></pre>
  
  <p>I autowire <code>PaymentTypeService</code> in a controller and then call <code>getAll()</code> method. But it still throws <code>NullPointerException</code>, here is whole stacktrace.</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  
  root cause
  
  java.lang.NullPointerException
      com.app.cloud.service.AbstractServiceImpl.getAll(AbstractServiceImpl.java:29)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
      com.sun.proxy.$Proxy207.getAll(Unknown Source)
      com.app.cloud.controller.orders.OrderController.edit(OrderController.java:150)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  </code></pre>
  
  <p>When I override <code>getAll</code> method (commented lines) in <code>PaymentTypeServiceImpl</code>, it works. Can you give me a advice?</p>