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.