Java – querydsl – case expression with string value
•
Java
QueryDsl 3.3. four
public class Document { private Confirmation confirmation; } public class Confirmation { ... }
I need this query:
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;
So it should be grouped according to the result of the case expression above Now, translate the case part into querydsl:
StringExpression confirmExp = new CaseBuilder() .when(Expressions.booleanTemplate("confirmation_id is null")) .then(Expressions.stringTemplate("NOT_CONFIRMED")) .otherwise(Expressions.stringTemplate("CONFIRMED"));
I'm using it When (expressions. Boolean template ("confirmation_id is null")) to avoid joining the confirmation table Running a query using such an expression, I get an exception below Is this another hibernate error or does this need to be different?
Solution
If you want to use string text in your query, you need to write it as
StringExpression confirmExp = new CaseBuilder() .when(Expressions.booleanTemplate("confirmation_id is null")) .then(Expressions.stringTemplate("'NOT_CONFIRMED'")) .otherwise(Expressions.stringTemplate("'CONFIRMED'"));
Expressions. Stringtemplate does not mean that the parameter is serialized as string literal, but the type of expression created is Java lang.String.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码