gpt4 book ai didi

spring - 将 ParameterExpression 与 org.springframework.data.jpa.domain.Specification 一起使用时如何将参数传递给函数?

转载 作者:行者123 更新时间:2023-12-02 10:19:13 25 4
gpt4 key购买 nike

我将 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 中的示例没有帮助。

最佳答案

我认为您真正需要的是一个可以使用 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 .

关于spring - 将 ParameterExpression 与 org.springframework.data.jpa.domain.Specification 一起使用时如何将参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49304067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com