title: Hibernate Paging
body: <p>I know through setMaxResults &amp; setFristResults method of Criteria in Hibernate i can do paging.. but my question is how do i separate it from DAO, I mean where i implement the DAO only there i am able to specify the setMaxResults() &amp; setFristResults() value, i want to make it as a separate layer,because if suppose there is around 500 DAO's various module then its difficult to change accordingly,</p>
  
  <p>Here is the code : 
  Domain : </p>
  
  <pre><code>public class Demo implements java.io.Serializable {
  private int id;
  private Date date;
  private String weekday;
  private Double cost;
  
  public Demo() 
  {
  
  }
  
  public Demo(Date date, String weekday, Double cost) 
  {
      this.date = date;
      this.weekday = weekday;
      this.cost = cost;
  }
  /***Setter &amp; getter***/
  }
  </code></pre>
  
  <p>DAO : </p>
  
  <pre><code>public interface DemoDAO {
  
  public void setPage(Page page);
  public List&lt;Demo&gt; findAll();
  }
  
  
  public class DemoDAOImpl implements DemoDAO  {
  private HibernateDAO hibernateDAO;
  private Session session;
  private Page page;
  
  public DemoDAOImpl()
  {
      hibernateDAO = new HibernateDAOImpl();
      session = hibernateDAO.getSession();
  }
  
  public void setPage(Page page) 
  {
      this.page = page;
  }
  
  public List&lt;Demo&gt; findAll() 
  {
      session.beginTransaction();
      Criteria criteria = session.createCriteria(Demo.class);
      criteria.setFirstResult(page.getPageNumber() * page.getPageSize());
      criteria.setMaxResults(page.getPageSize());
  
      List&lt;Demo&gt; demoList = criteria.list();
      session.getTransaction().commit();
  
      return demoList;
  }
  </code></pre>
  
  <p>Service :</p>
  
  <pre><code>public interface DemoService {
  
  public List&lt;Demo&gt; showDetails(Page page);
  }
  
  
  
  public class DemoServiceImpl implements DemoService {
  private DemoDAO demoDAO;
  
  public DemoServiceImpl()
  {
      demoDAO = new DemoDAOImpl();
  }
  
  public List&lt;Demo&gt; showDetails(Page page) 
  {
      if(page != null)
          demoDAO.setPage(page);
      else
          page = new Page(0,50);
  
      return demoDAO.findAll();
  }
  }
  </code></pre>
  
  <p>now from the frontend i am calling this showDetails... </p>
  
  <p>please correct me if i am using the correct thing...</p>