加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – QueryDsl – 具有字符串值的case表达式

发布时间:2020-12-14 05:58:24 所属栏目:Java 来源:网络整理
导读:QueryDsl 3.3.4 Hibernate 3.6.10-最终版 我有两个实体: public class Document { private Confirmation confirmation;}public class Confirmation { ...} 我需要这样的查询: SELECT count(d.id),CASE WHEN d.confirmation_id IS NULL then 'NOT_CONFIRMED
QueryDsl 3.3.4
Hibernate 3.6.10-最终版
我有两个实体:
public class Document {
    private Confirmation confirmation;
}

public class Confirmation {
    ...
}

我需要这样的查询:

SELECT count(d.id),CASE WHEN d.confirmation_id IS NULL then 'NOT_CONFIRMED' else 'CONFIRMED' END as confirmed FROM document d GROUP BY confirmed;

所以它应该按照上面的case表达式的结果进行分组.
现在,将案例部分翻译为querydsl:

StringExpression confirmExp = new CaseBuilder()
    .when(Expressions.booleanTemplate("confirmation_id is null"))
    .then(Expressions.stringTemplate("NOT_CONFIRMED"))
    .otherwise(Expressions.stringTemplate("CONFIRMED"));

我正在使用.when(Expressions.booleanTemplate(“confirmation_id为null”))以避免加入确认表.
使用这样的表达式运行查询我在下面得到一个例外.
这是另一个hibernate错误或这种情况需要不同吗?

java.lang.IllegalStateException: No data type for node: >org.hibernate.hql.ast.tree.CaseNode
+-[CASE] CaseNode: ‘case’
| +-[WHEN] SqlNode: ‘when’
| | +-[IS_NULL] IsNullLogicOperatorNode: ‘is null’
| | | -[IDENT] IdentNode: ‘confirmation_id’ {originalText=confirmation_id}
| | -[IDENT] IdentNode: ‘NOT_CONFIRMED’ {originalText=NOT_CONFIRMED}
| -[ELSE] SqlNode: ‘else’
| -[IDENT] IdentNode: ‘CONFIRMED’ {originalText=CONFIRMED}

org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:156)

解决方法

如果您想在查询中使用字符串文字,则需要将其写为
StringExpression confirmExp = new CaseBuilder()
    .when(Expressions.booleanTemplate("confirmation_id is null"))
    .then(Expressions.stringTemplate("'NOT_CONFIRMED'"))
    .otherwise(Expressions.stringTemplate("'CONFIRMED'"));

Expressions.stringTemplate并不意味着参数被序列化为String文字,但创建的表达式的类型为java.lang.String.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读