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.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...