【问题标题】:How to pass an argument to a function when using ParameterExpression together with org.springframework.data.jpa.domain.Specification?将ParameterExpression与org.springframework.data.jpa.domain.Specification一起使用时如何将参数传递给函数?
【发布时间】:2018-08-24 12:52:30
【问题描述】:

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

问题是不清楚如何为这个函数传递参数,因为我没有使用 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);

here 的示例没有帮助。

【问题讨论】:

    标签: spring jpa spring-data-jpa criteria-api


    【解决方案1】:

    我认为您真正需要的是可以使用CriteriaBuilder.literal 创建的文字。一个完整的例子看起来像

    // 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) 任何 functions returning an Expression

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-10
      • 2014-06-04
      相关资源
      最近更新 更多