多种谓词规定与JpAspecification Executor的动态组合以及MongoDB的微生物数据

发布于 2025-01-19 22:54:34 字数 1545 浏览 6 评论 0原文

如何使用 MongoDB 中的 JpaSpecificationExecutor 和 Micronaut 数据附加所有 PredicateSpecification 或 QuerySpecification?

谓词

public class DiscountSpecification {
    final static BeanIntrospection<Discount> introspection = BeanIntrospection.getIntrospection(Discount.class);
    static String[] discountProperty = introspection.getPropertyNames();

    public static PredicateSpecification<Discount> DiscountIdEquals(ObjectId DiscountId) {
        return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[0]), DiscountId);
    }

    public static PredicateSpecification<Discount> DiscountCodeEquals(String DiscountCode) {
        return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[1]), DiscountCode);
    }
}

现在根据条件我想附加所有谓词

@Introspected
public record QueryBuilderSpecification() {
    public static PredicateSpecification<Discount> QueryBuilder(DiscountFilterModel discountFilterModel){
        PredicateSpecification<Discount> predicateSpecification = null;

        if (discountFilterModel.getId() != null){
            predicateSpecification =  DiscountIdEquals(new ObjectId(discountFilterModel.getId()));
        }
        if (discountFilterModel.getDiscountCode() != null){
            predicateSpecification =  DiscountCodeEquals(discountFilterModel.getDiscountCode());
        }
        return predicateSpecification;
    }
}

上面的代码不会附加这两个条件。非常不确定如果两者都存在的话如何将两者结合起来。

How can I append all the PredicateSpecification or QuerySpecification using JpaSpecificationExecutor in MongoDB with Micronaut data?

Predicate

public class DiscountSpecification {
    final static BeanIntrospection<Discount> introspection = BeanIntrospection.getIntrospection(Discount.class);
    static String[] discountProperty = introspection.getPropertyNames();

    public static PredicateSpecification<Discount> DiscountIdEquals(ObjectId DiscountId) {
        return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[0]), DiscountId);
    }

    public static PredicateSpecification<Discount> DiscountCodeEquals(String DiscountCode) {
        return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[1]), DiscountCode);
    }
}

Now based on the condition I want to append all the predicate

@Introspected
public record QueryBuilderSpecification() {
    public static PredicateSpecification<Discount> QueryBuilder(DiscountFilterModel discountFilterModel){
        PredicateSpecification<Discount> predicateSpecification = null;

        if (discountFilterModel.getId() != null){
            predicateSpecification =  DiscountIdEquals(new ObjectId(discountFilterModel.getId()));
        }
        if (discountFilterModel.getDiscountCode() != null){
            predicateSpecification =  DiscountCodeEquals(discountFilterModel.getDiscountCode());
        }
        return predicateSpecification;
    }
}

The above code doesn't append for both the condition. Quite not sure how to combine both if both are present.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂归处 2025-01-26 22:54:34

应该可以使用discoundidequal(...)和(discountCodeequals(..))

您的示例可以与predicatesPecification.lall作为初始状态一起使用,然后具有:

PredicatesPecification = PredicatesPecification..ans and(discountidequals(...))

测试应用程序中有一些示例:
https://github.com/micronaut-projects/micronaut-data/blob/master/master/doc-examples/mongo-example-example-java/src/src/test/java/java/java/java/java/java/example/example/personrepersamplepempampleperpamplesitoryspecoritoryspec.java-java,javal50 < /a>

It should be possible to use DiscountIdEquals(...).and(DiscountCodeEquals(..)).

Your example can be used with PredicateSpecification.ALL as the initial state and then have:

predicateSpecification = predicateSpecification.and(DiscountIdEquals(...)).

There are some examples in the test apps:
https://github.com/micronaut-projects/micronaut-data/blob/master/doc-examples/mongo-example-java/src/test/java/example/PersonRepositorySpec.java#L50

北陌 2025-01-26 22:54:34

基于代码 PredicateSpecification 你可以 & 规格在一起。如 Micronaut 数据用户指南中所示

Based on the code PredicateSpecification you can and & or specifications together. As demonstrated in the Micronaut Data User Guide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文