SQL 'case when' in hibernate Criteria

孤街醉人 提交于 2020-01-15 12:35:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!