I do have a static metamodel with a NUMBER (actually, BigDecimal, don't ask why) column. Now I would like to do a LIKE query against that number column:
CriteriaBuilder cb;
cb.like(entity.get(Entity_.numbercol), "123%");
where entity.get(Entity_.numbercol)
returns a Path<BigDecimal>
. Naturally, I get a compile error: ...like(Expression<String>, ...) ... not applicable for the arguments (Path<BigDecimal>, ...)
Casting the column with .as(String.class)
fails due to some Bug in JPA, but I don't have the bug number at hands right now. It is not fixed in the latest version of JPA/Hibernate, though. Anyway, it results in a runtime exception about some invalid SQL statement being generated.
Now I just need a way to get the criteria API equivalent of the SQL
... WHERE numbercol LIKE '123%';
Searching for the topic already brought up the following responses, which don't help because I have a static metamodel: NHibernate - easiest way to do a LIKE search against an integer column with Criteria API? and JPA/Criteria API - Like & equal problem
Any ideas?
Thanks in advance Dominik
No, it does not fail because of bug, it works as specified (for example in Javadoc):
Method you use performs cast and you need conversion. In general there is no support to convert from BigDecimal to String in JPA.
For the people, who are still looking for a solution.
Knowing that HQL
str
function does the job (at least for Hibernate v. 3.6.9.Final) one can implement his ownFunctionExpression
like:and then use it: