微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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.

原文地址:https://www.jb51.cc/java/120559.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐