title: Can i use generic method to acess data from different entities in hibernate
body: <p>For example: i have User.class and Post.Class
  I want to get all rows from this tables with hibernate.
  It is done with </p>
  
  <pre><code>TypedQuery&lt;Post&gt; query = SessionFactory.getCurrentSession().createQuery("from Post");
  </code></pre>
  
  <p>Also i have dao layer, UserDao and PostDao. and i have abstract class CrudDao with methods like:</p>
  
  <pre><code>public abstract class CrudDao&lt;T&gt; {
  
  
      @Transactional
      public void save(T entity) {
          SessionFactory.getCurrentSession().save(entity);
      }
  }
  </code></pre>
  
  <p>So my user and post dao just extend this crudDao and i don't need to write save method for them. When i want to get all rows i need to write "from MyEntitie". But i want to make this method also abstract, so i don't need to write it multiple times for each dao. But i can't write "from T" in abstract method.</p>
  
  <p>I also tried to do like this:</p>
  
  <pre><code>List&lt;T&gt; getAll(Class&lt;T&gt; type){
      CriteriaQuery&lt;T&gt; criteria = builder.createQuery(type);
      criteria.from(type);
      return 
    SessionFactory.getCurrentSession().createQuery(criteria).getResultList();
  }
  </code></pre>
  
  <p>So in my service i call my dao like this:</p>
  
  <pre><code>PostDao.getAll(Post.class);
  </code></pre>
  
  <p>And i was told that my service shouldn't know about my entity and my dao call in service should be 
  <code>PostDao.getAll();</code>
  And if i do like this ^ i need to write getAll method in every dao and it looks like a lot of copypasted code. </p>
  
  <p>Can yoy give me some advice on how to do it or how you do it in your projects?</p>