Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casebuilder could not determine data type for searched case statement #1968

Closed
michaelhum opened this issue Jul 13, 2016 · 5 comments
Closed

Comments

@michaelhum
Copy link

michaelhum commented Jul 13, 2016

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.

@ntantri
Copy link
Contributor

ntantri commented Jul 14, 2016

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)

@michaelhum
Copy link
Author

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

@ntantri
Copy link
Contributor

ntantri commented Jul 15, 2016

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

@Shredder121
Copy link
Member

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.

@michaelhum
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants