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()]);  
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。