JPA: How to perform a LIKE with a NUMBER column in a static JPA MetaModel?

依然范特西╮ 提交于 2019-12-01 06:08:20

问题


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


回答1:


No, it does not fail because of bug, it works as specified (for example in Javadoc):

Perform a typecast upon the expression, returning a new expression object. This method does not cause type conversion: the runtime type is not changed. Warning: may result in a runtime failure.

Method you use performs cast and you need conversion. In general there is no support to convert from BigDecimal to String in JPA.




回答2:


For the people, who are still looking for a solution.

Knowing that HQLstr function does the job (at least for Hibernate v. 3.6.9.Final) one can implement his own FunctionExpression like:

//plagiarized from org.hibernate.ejb.criteria.expression.function.CastFunction
public class StrFunction<Y extends Number> extends BasicFunctionExpression<String> implements FunctionExpression<String>, Serializable {
    public static final String FCT_NAME = "str";

    private final Selection<Y> selection;

    public StrFunction(CriteriaBuilder criteriaBuilder, Selection<Y> selection) {
        super((CriteriaBuilderImpl) criteriaBuilder, String.class, FCT_NAME);
        this.selection = selection;
    }

    @Override
    public void registerParameters(ParameterRegistry registry) {
        Helper.possibleParameter(selection, registry);
    }

    @Override
    public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
        return FCT_NAME + '(' + ((Renderable) selection).render(renderingContext) + ')';
    }
}

and then use it:

cb.like(new StrFunction<Long> (cb, root.get(MyObject_.id)), "%mySearchTerm%");


来源:https://stackoverflow.com/questions/9802224/jpa-how-to-perform-a-like-with-a-number-column-in-a-static-jpa-metamodel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!