多种谓词规定与JpAspecification Executor的动态组合以及MongoDB的微生物数据
如何使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该可以使用
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
基于代码 PredicateSpecification 你可以
和
&或
规格在一起。如 Micronaut 数据用户指南中所示Based on the code PredicateSpecification you can
and
&or
specifications together. As demonstrated in the Micronaut Data User Guide