如何使用条件在Hibernate中实现选择和计数

时间:2018-10-11 03:27:46

标签: java spring hibernate

我是休眠的新手,我有类似的查询:

select count(1) from (
SELECT COUNT (1)
FROM USR_BASE
WHERE ST_CD = 1
group by USR_NO)

如何使用条件在Hibernate中实现该查询?

因为,我已经使用方法实现了:

public int totalUser(UsrBase usrBase) {
    Criteria criteria = createCriteria();
    String stCd = usrBase.getStCd();
    criteria.setProjection(Projections.projectionList())
            .add(Projections.property("usrNo"))
            .add(Projections.property(stCd))
            .add(Projections.groupProperty("usrNo")));

    return((Long)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();

}

结果与我的查询不同...请帮助我。

1 个答案:

答案 0 :(得分:1)

select count(1) from (
SELECT COUNT (1)
FROM USR_BASE
WHERE ST_CD = 1
group by USR_NO)

我认为使用它会更容易

select count(distinct(USR_NO)) from USR_BASE WHERE ST_CD = 1