anggaerlangga
anggaerlangga

Reputation: 1

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.

Upvotes: 0

Views: 76

Answers (1)

Huy Nguyen
Huy Nguyen

Reputation: 2061

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

Upvotes: 1

Related Questions