spring-data-jpa 将IN子句添加到连接规范

eit6fx6z  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(151)

这是现有的代码,我需要传递订单类型的列表而不是一个orderType。并且这个规范已经添加到现有搜索查询的WHERE子句中。
如何修改IN子句的现有规范?

public static Specification<OrderEntity> addReference(String orderType, String refNum) {
  return (root, query, builder) -> {
    if (null != refType && null != refNum) {
      Join<OrderEntity, OrderDetailEntity> orderSearchJoin =
                root.join(OrderEntity_.orderDetailEntityList);
      query.distinct(true);      
      return builder.and(
                builder.equal(orderSearchJoin.get(OrderDetailEntity_.orderType), orderType),
                builder.equal(orderSearchJoin.get(OrderDetailEntity_.referenceNum), refNum));
    } else {
      return null;
    }
  };
}
oaxa6hgo

oaxa6hgo1#

这可能是您问题的答案:

public static Specification<OrderEntity> addReference(List<String> orderTypes, List<String> refNums) {
        return (root, query, builder) -> {
            if (null != orderTypes && !orderTypes.isEmpty() && null != refNums && !refNums.isEmpty()) {
                Join<OrderEntity, OrderDetailEntity> orderSearchJoin =
                        root.join(OrderEntity_.orderDetailEntityList);
                query.distinct(true);
                return builder.and(orderSearchJoin.get(OrderDetailEntity_.orderType).in(orderTypes),
                        orderSearchJoin.get(OrderDetailEntity_.referenceNum).in(refNums));
            } else {
                return null;
            }
        };
    }

参考编号:
https://docs.oracle.com/javaee/7/api/javax/persistence/criteria/Join.htmlhttps://docs.oracle.com/javaee/7/api/javax/persistence/criteria/CriteriaBuilder.html显示器

相关问题