title: Spring Generic Dao class name
body: <p>I have configured a custom generic service DAO for my spring / hibernate project - the idea being that I can reuse it easily from my controllers.</p>
  
  <p>It essentially looks like this:</p>
  
  <pre><code>public class DefaultService&lt;T&gt; {
  
  private Class&lt;T&gt; e;
  
  public String className(Class&lt;T&gt; e) {
      String clip = e.getName();
      clip = clip.substring(clip.lastIndexOf('.') + 1, clip.length());
      return clip;
  }
  public List&lt;T&gt; getAll(Integer status) {
      Session session = sessionFactory.getCurrentSession();
      Query query = session.createQuery("FROM " + className(e) + " WHERE status = " + status);
      return query.list();
  }
  ...
  </code></pre>
  
  <p>Which gets referenced by:</p>
  
  <pre><code>@Autowired
  public DefaultService&lt;Address&gt; addressService;
  addressService.get(1);
  </code></pre>
  
  <p>However the <code>String clip = e.getName()</code> line throws a Null pointer exception. I can get this to work if I move the class into the attributes section (so <code>addressService.get(Address.class, 1)</code> but I find this somewhat untidy, especially when there are multiple different classes being called upon. </p>
  
  <p>Is there some way to get the class to generate a value correctly without repeatedly adding it into all my functions?</p>
  
  <p>Thanks in advance.</p>