簡體   English   中英

QueryDsl - 具有字符串值的case表達式

[英]QueryDsl - case expression with string value

QueryDsl 3.3.4
Hibernate 3.6.10-Final我有兩個實體:

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 is null"))以避免加入confirmation表。 使用這樣的表達式運行查詢我在下面得到一個例外。
這是另一個hibernate錯誤或這種情況需要不同嗎?

java.lang.IllegalStateException:沒有節點的數據類型:> org.hibernate.hql.ast.tree.CaseNode + - [CASE] CaseNode:'case'| + - [WHEN] SqlNode:'when'| | + - [IS_NULL] IsNullLogicOperatorNode:'為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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM