在'hibernate Criteria'中'SQL'的情况 [英] SQL 'case when' in hibernate Criteria

查看:102
本文介绍了在'hibernate Criteria'中'SQL'的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  select pr_name,count(*)from(select()如果serv.type = xyz,那么serv.nameA else serv.nameB end)作为pr_name从db.serv serv其中serv.date是null group by pr_name; 

我已经得到了以下内容来处理其余部分(个案部分除外)

 currentSession.createCriteria(StoredData.class)
.setProjection(projectionList()
.add(groupProperty(pr_name),pr_name)
.add(rowCount(),count))
.add(isNull(date))
.setResultTransformer(new PrCountTransformer())
.list();


解决方案

您可以创建另一个属性 prName StoredData ,并使用注释 @Formula 定义这个case部分。 p>

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

解决方案

You can create another property prName in StoredData and define this "case" part with pure sql using annotation @Formula.

这篇关于在'hibernate Criteria'中'SQL'的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆