简体   繁体   中英

How can I implement Select and Count in Hibernate using criteria

I'm new using hibernate, I have query like :

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

How can I implement that query in Hibernate using criteria ?

Because, I already implement with method :

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

}

the result not same with my query... Please help me.

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

I think it will be more easily with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM