Projections.rowCount不会返回唯一结果

时间:2017-03-17 06:18:52

标签: hibernate criteria projection rowcount

我有以下代码。我需要计算此条件返回的总行数。我有许多谓词,我已经实现了限制。我使用了具有组属性的投影列表和带有条件的行计数,但它没有返回唯一的结果。我错过了什么:

public Integer getSurveyCount(
        Date from,
        Date to,
        Integer districtId,
        Integer tehsilId,
        Integer qanungoiHalqaId,
        Integer patwarCircleId,
        Integer mauzaId,
        Integer surveyorId,
        Integer surveyTypeId,
        Integer verified) {

    Session session = sessionFactory.getCurrentSession();

    Criteria criteria = session.createCriteria(Survey.class);

    criteria.addOrder(Order.asc("surveyId"));

    if (from != null && to != null) 
        criteria.add(Restrictions.between("surveyDatetime", from, to));
    else if (from != null && to == null)
        criteria.add(Restrictions.ge("surveyDatetime", from));
    else if (from == null && to != null)
        criteria.add(Restrictions.le("surveyDatetime", to));

    if (verified == 1)
        criteria.add(Restrictions.eq("verified", true));
    if (verified == 2)
        criteria.add(Restrictions.eq("verified", false));

    if (surveyTypeId != null && surveyTypeId != 0) 
        criteria.add(Restrictions.eq("surveyType.surveyTypeId", surveyTypeId ));

    if (surveyorId != null && surveyorId != 0){
        criteria.add(Restrictions.eq("surveyor.surveyorId", surveyorId));
    }

    if
    (mauzaId != null && mauzaId != 0)
        criteria.createCriteria("location")
                .createCriteria("mauza")
                .add(Restrictions.eq("mauzaId", mauzaId));
    else if 
    (patwarCircleId != null && patwarCircleId != 0)
        criteria.createCriteria("location")
        .createCriteria("mauza")
        .createCriteria("patwarCircle")
        .add(Restrictions.eq("patwarCircleId", patwarCircleId));
    else if
    (qanungoiHalqaId != null && qanungoiHalqaId != 0)
        criteria.createCriteria("location")
        .createCriteria("mauza")
        .createCriteria("patwarCircle")
        .createCriteria("qanungoiHalqa")
        .add(Restrictions.eq("qanungoiHalqaId", qanungoiHalqaId));
    else if
    (tehsilId != null && tehsilId !=0)
        criteria.createCriteria("location")
        .createCriteria("mauza")
        .createCriteria("patwarCircle")
        .createCriteria("qanungoiHalqa")
        .createCriteria("tehsil")
        .add(Restrictions.eq("tehsilId", tehsilId));
    else if
    (districtId != null && districtId != 0)
        criteria.createCriteria("location")
                .createCriteria("mauza")
                .createCriteria("patwarCircle")
                .createCriteria("qanungoiHalqa")
                .createCriteria("tehsil")
                .createCriteria("district")
                .add(Restrictions.eq("districtId", districtId));


    criteria.setProjection(
            Projections.projectionList()
            .add(Projections.groupProperty("surveyId"))
            .add(Projections.rowCount()));

    return (Integer) criteria.uniqueResult();
}

0 个答案:

没有答案