Hibernate rowcount / Pagination无法正常工作

时间:2015-07-24 02:07:20

标签: java hibernate

我的分页不起作用..

第一种方法应返回2行,第二种方法应返回12行。但它只返回10行..

需要一些帮助。这是代码。

addEftNonMyWaterBillsToVO(request, output);
addEftMyWaterBillsToVO(request, output);


private void addEftNonMyWaterBillsToVO(EftBillSearchVO request, EftManagementBillSearchResultVO vo) {

    Criteria eftNonMyWaterBillCriteria = this.getCurrentSession().createCriteria(EftNonMyWaterBill.class, "eftNonMyWaterBill");

    Criteria eftSubmissionCriteria1 = eftNonMyWaterBillCriteria.createCriteria("submission", "eftSubmission");
    eftSubmissionCriteria1.add(Restrictions.eq("id", request.getEftSubmissionId()));

    if (request.getAccountNumber() != null) {
        eftNonMyWaterBillCriteria.add(Restrictions.eq("accountNumber", request.getAccountNumber()));//
    }

    if (request.getBillId() != null) {
        eftNonMyWaterBillCriteria.add(Restrictions.eq("billId", Long.valueOf(request.getBillId()).intValue()));
    }

    if (request.getStatus() != null) {
        eftNonMyWaterBillCriteria.add(Restrictions.eq("status", request.getStatus()));
    }

    // Do a projection to get the row count
    eftSubmissionCriteria1.setProjection(Projections.rowCount());
    vo.setTotalResults(((Long) eftSubmissionCriteria1.uniqueResult()).intValue());
    eftSubmissionCriteria1.setProjection(null);

    // Set up the retrieval specifics
    // TODO: Ordering?
    eftSubmissionCriteria1.setFirstResult(request.getStartIndex());
    if (request.getMaxResults() > 0) {
        eftSubmissionCriteria1.setMaxResults(request.getMaxResults());
    }

    // Set up the projections to map data onto the EftSubmissionVO
    ProjectionList projections = Projections.projectionList()
            .add(Projections.alias(Projections.property("id"), "eftBillId"))
            .add(Projections.alias(Projections.property("accountNumber"), "accountNumber"))
            .add(Projections.alias(Projections.property("accountAddress"), "accountAddress"))
            .add(Projections.alias(Projections.property("billId"), "billIdInt"))
            .add(Projections.alias(Projections.property("billType"), "nonMyWaterBillType"))
            .add(Projections.alias(Projections.property("amount"), "amount"))
            .add(Projections.alias(Projections.property("status"), "status"))
            .add(Projections.alias(Projections.property("statusDate"), "processedDate"))
            .add(Projections.alias(Projections.property("suspenseAccount"), "suspenseAccount"));

    @SuppressWarnings("unchecked")
    List<EftManagementBillVO> eftBills = eftNonMyWaterBillCriteria.setProjection(projections).setResultTransformer(Transformers.aliasToBean(EftManagementBillVO.class)).list();

    if (eftBills != null) {
        for (EftManagementBillVO eftManagementBillVO : eftBills) {
            eftManagementBillVO.setNonMyWaterBill(true);
        }

        vo.setResults(eftBills);
    }
}

private void addEftMyWaterBillsToVO(EftBillSearchVO request, EftManagementBillSearchResultVO vo) {

    Criteria eftBillCriteria = this.getCurrentSession().createCriteria(EftBill.class, "eftBill");

    Criteria eftSubmissionCriteria = eftBillCriteria.createCriteria("submission", "eftSubmission");
    eftSubmissionCriteria.add(Restrictions.eq("id", request.getEftSubmissionId()));

    Criteria billCriteria = eftBillCriteria.createCriteria("bill", "bill");

    if (request.getAccountNumber() != null) {
        billCriteria.add(Restrictions.like("ern", String.valueOf(request.getAccountNumber() + "%")));//
    }

    if (request.getBillId() != null) {
        String billId = StringUtils.leftPad(request.getBillId().toString(), 4, "0");
        billCriteria.add(Restrictions.eq("billId", billId));
    }

    if (request.getStatus() != null) {
        eftBillCriteria.add(Restrictions.eq("status", request.getStatus()));
    }

    // Do a projection to get the row count
    eftSubmissionCriteria.setProjection(Projections.rowCount());
    vo.setTotalResults(vo.getTotalResults() + ((Long) eftSubmissionCriteria.uniqueResult()).intValue());
    eftSubmissionCriteria.setProjection(null);

    // Set up the retrieval specifics
    // TODO: Ordering?
    eftSubmissionCriteria.setFirstResult(request.getStartIndex());
    if (request.getMaxResults() > 0) {
        eftSubmissionCriteria.setMaxResults(request.getMaxResults());
    }

    // Set up the projections to map data onto the EftSubmissionVO
    ProjectionList projections = Projections.projectionList()
            .add(Projections.alias(Projections.property("id"), "eftBillId"))
            .add(Projections.alias(Projections.property("bill.ern"), "ern"))
            .add(Projections.alias(Projections.property("bill.addressDescription"), "accountAddress"))
            .add(Projections.alias(Projections.property("bill.billId"), "billId"))
            .add(Projections.alias(Projections.property("bill.billType"), "billType"))
            .add(Projections.alias(Projections.property("amount"), "amount"))
            .add(Projections.alias(Projections.property("status"), "status"))
            .add(Projections.alias(Projections.property("statusDate"), "processedDate"))
            .add(Projections.alias(Projections.property("suspenseAccount"), "suspenseAccount"));

    @SuppressWarnings("unchecked")
    List<EftManagementBillVO> eftBills = eftBillCriteria.setProjection(projections)
            .setResultTransformer(Transformers.aliasToBean(EftManagementBillVO.class)).list();

    if (vo.getResults() != null && eftBills != null) {
        vo.getResults().addAll(eftBills);
    }
}

0 个答案:

没有答案