Querydsl: Casebuilder could not determine data type for searched case statement

Created on 13 Jul 2016  ·  5Comments  ·  Source: querydsl/querydsl

The old code:

new CaseBuilder() .when(foo.id.eq(NumberTemplate.create(Integer.class, "1").then(BooleanTemplate.FALSE) .when(foo.id.eq(NumberTemplate.create(Integer.class, "2").then(BooleanTemplate.TRUE) .otherwise(new NullExpression<>(Boolean.class)

I've tried replacing it with

new CaseBuilder() .when(foo.id.eq(Expressions.asNumber(1).then(false) .when(foo.id.eq(Expressions.asNumber(2)).then(true) .otherwise(Expressions.nullExpression(Boolean.class))

which didn't work. I get the error

org.hibernate.QueryException: Could not determine data type for searched case statement

I then tried using Expressions.constant(), which also didn't work. Finally, I tried Expressions.asBoolean() and Expressions.FALSE, which doesn't compile due to ambiguous method call: valid in Predicate and ComparableExpression.

I should note that explicitly casting Expressions.FALSE to Expression does allow the code to compile (and generate valid SQL), although now there are unchecked warnings.

hibernate resolved

Most helpful comment

Hibernate doesn't know what type ? is in the query, but needs to know in case expressions.

Using templates like the answer on stackoverflow is an option.
It makes us serialize whatever is in the template as-is.

Other than that, it's a hibernate issue.

All 5 comments

Somehow, the syntax you mentioned is not fitting, could you try this:

new CaseBuilder()
.when(foo.id.eq(Expressions.asNumber(1))).then(false)
.when(foo.id.eq(Expressions.asNumber(2))).then(true)
.otherwise(Expressions.nullExpression(Boolean.class))

See the extra ) after Expressions.asNumber(1)

My apologies - typo with copying over the code. I still get the same result.

I guess you can give a try for this solution that was related to Hibernate doing something with lietrals: http://stackoverflow.com/questions/26648491/querydsl-could-not-determine-data-type-for-searched-case-statement#answer-27190771

Hibernate doesn't know what type ? is in the query, but needs to know in case expressions.

Using templates like the answer on stackoverflow is an option.
It makes us serialize whatever is in the template as-is.

Other than that, it's a hibernate issue.

Hi,

I'll give it a try - although I've tried numerous templates. Is there a specific one? In 3.x I was using NumberTemplate and BooleanTemplate but in 4 I believe it is now different?

I've tried:

Expressions.asBoolean()
Expressions.constant()
Expressions.FALSE

As I did mention though, this syntax:

.when(Expressions.asNumber(1))
.then(Expressions.asBoolean(false))
.when(Expressions.asNumber(2))
.then(Expressions.asBoolean(true))
.otherwise(Expressions.nullExpression(Boolean.class))

results in a compilation error (ambiguous method call with '.then(Expressions.asBoolean(false))' - both overridden in ComparableExpression and Predicate apply. I can make it work by casting the result to Expression, is this expected behaviour?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pascal-hofmann picture pascal-hofmann  ·  4Comments

intuitiveminds picture intuitiveminds  ·  8Comments

Ghalleb picture Ghalleb  ·  4Comments

robertandrewbain picture robertandrewbain  ·  5Comments

d-schmidt picture d-schmidt  ·  5Comments