1

I have a numeric id field in an entity that I want to concatenate to some other string. Right now I'm doing:

cb.concat(
  from.get("id").as(String.class)
  , otherString
)

That works and it is translated to CAST(mytable.id AS VARCHAR(255).

I want to be able to cast it to a varchar of lenght 20 VARCHAR(20), because the performance of the query improves exponentially.

Any idea on how to do it?

gmc
  • 3,910
  • 2
  • 31
  • 44
  • This is not possible. You could try to call function and pass the cast function and it's parameters. But the question is why do you concatenate on the DB side and not in Java? – Simon Martinelli Jan 11 '18 at 11:25
  • Because I need the concatenation as a condition for the query, not to format the results. I've tried using `cb.function` with `CAST`, but I always end up building wrong the expression – gmc Jan 11 '18 at 11:27
  • then you probably should execute it as SQL native query – Simon Martinelli Jan 11 '18 at 11:32
  • Aight, thanks anyway – gmc Jan 11 '18 at 11:33
  • At least in hibernate you can register a custom function https://www.thoughts-on-java.org/database-functions/ and use it with the criteria api https://www.thoughts-on-java.org/hibernate-tips-call-custom-function-criteriaquery/ Of course, the question is: is it worth the hassle? – Jens Schauder Jan 11 '18 at 12:50

0 Answers0