首页 > 代码库 > Hibernate 分页

Hibernate 分页

public EmailBean[] findByPage(Date begin, Date end, String emailreference, int pageNo, final int pageSize, String status) throws AIException {          List emails = new ArrayList();            try {              Session s = getSessionFactory().getCurrentSession();                Criteria hcriteria = s.createCriteria(EmailBean.class);                if (begin != null && end != null) {                  hcriteria.add(Restrictions.between("creation", begin, end));              }                if (emailreference != null && !emailreference.isEmpty()) {                  hcriteria.add(Restrictions.ilike("mailName", emailreference));              }                if (status != null && !status.isEmpty()) {                  hcriteria.createAlias("status", "s");                  hcriteria.add(Restrictions.eq("s.statusname", status));              }              //Total record number              int totalCount = ((Integer) hcriteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();              hcriteria.setProjection( null );                hcriteria.addOrder(Order.desc("creation")).setFirstResult((pageNo - 1)*pageSize).setMaxResults(pageSize);                emails = hcriteria.list();              Hibernate.initialize(emails);          } catch (Exception e) {              _logger.error(e.fillInStackTrace());              throw new AIException("Error when retrieving emails");          }            return emails.toArray(new EmailBean[emails.size()]);      }

 

Hibernate 分页