问题
How can I tranform the following query to a hibernate Criteria?
select pr_name, count(*) from (select (case when serv.type=xyz then serv.nameA else serv.nameB end) as pr_name from db.serv serv where serv.date is null group by pr_name;
I have got the following to handle the rest (except for the case part)
currentSession.createCriteria(StoredData.class)
.setProjection(projectionList()
.add(groupProperty("pr_name"), "pr_name")
.add(rowCount(), "count"))
.add(isNull("date"))
.setResultTransformer(new PrCountTransformer())
.list();
回答1:
You can create another property prName
in StoredData
and define this "case" part with pure sql using annotation @Formula
.
来源:https://stackoverflow.com/questions/16515868/sql-case-when-in-hibernate-criteria