0

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.

3
  • 1
    your sql query isn't so clear as what exactly you are trying to achieve. There is no point of using group by as at the end your are counting entire result. Commented Oct 11, 2018 at 3:35
  • Agree with the above comment. Your query makes no sense, so there is no point to asking about Criteria syntax. Commented Oct 11, 2018 at 3:37
  • how about this ? select count(*) from ( select usr_mgnt_no from igib_usr_base where st_cd = 1 group by usr_mgnt_no) Commented Oct 11, 2018 at 3:38

1 Answer 1

1
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
Sign up to request clarification or add additional context in comments.

1 Comment

i am glad it help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.