将ParameterExpression与org.springframework.data.jpa.domain.Specification一起使用时,如何将参数传递给函数? [英] How to pass an argument to a function when using ParameterExpression together with org.springframework.data.jpa.domain.Specification?

查看:231
本文介绍了将ParameterExpression与org.springframework.data.jpa.domain.Specification一起使用时,如何将参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用org.springframework.data.jpa.domain.Specification和JpaSpecificationExecutor来轻松地在Java中创建带有条件的查询,但是现在我需要调用一个返回整数值的MySQL DB函数.

I am using org.springframework.data.jpa.domain.Specification together with JpaSpecificationExecutor to easily create queries with conditions in Java but now I need to call a MySQL DB function that returns an integer value.

问题在于,由于我不使用TypedQuery,因此尚不清楚如何为此函数传递参数:

The problem is that it is unclear how to pass an argument for this function as I am not using TypedQuery:

// cb here is CriteriaBuilder
ParameterExpression param = cb.parameter(String.class);

Predicate predicate = cb.greaterThan(cb.function("A_FUNCTION", 
      Integer.class, param), 0);

Specification spec = cb.and(predicate);

// query is executed like this

return (int) repositoryThatExtendsJpaSpecificationExecutor.count(test);

此处中的示例>没有帮助.

And the example from here is not helping.

推荐答案

我认为您真正需要的是可以使用

I think what you really need is a literal which you can create using CriteriaBuilder.literal. A full example would look like

// cb here is CriteriaBuilder
Expression param = cb.literal("Stephen Hawking");

Predicate predicate = cb.greaterThan(cb.function("A_FUNCTION", 
      Integer.class, param), 0);

Specification spec = cb.and(predicate);

如果该值不是来自您的应用程序,则可以使用(m)任何

If the value doesn't come from your application you can use (m)any of the functions returning an Expression.

这篇关于将ParameterExpression与org.springframework.data.jpa.domain.Specification一起使用时,如何将参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆