提取并过滤规格

时间:2018-08-12 23:34:47

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

我使用spring boot2。我创建了一个高级搜索,可以搜索许多条件,然后用户选择。

public Page<Samples> advancedSearch(SamplesSearchDto search, Pageable page) {
    Specification<Samples> specification = (Root<Samples> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {

        Predicate p = cb.conjunction();
        Join<Samples, Samplings> samplings = root.join("sampling", JoinType.INNER);
        Join<Samples, Colors> colors = (Join) samplings.join("color");
        Join<Samples, Dimensions> dimension = (Join) samplings.join("dimension");
        Join<Samplings, Products> product = (Join) samplings.join("product", JoinType.INNER);

        Join<Products, ProductTypes> productType = (Join) product.join("productType", JoinType.INNER);

        Join<Samplings, Machines> machine = (Join) samplings.join("machine", JoinType.LEFT); //left

        Join<Samples, GranulateSamplings> granulateSamplings = cb.treat(samplings, GranulateSamplings.class);

        Join<GranulateSamplings, Suppliers> supp = granulateSamplings.join("supplier", JoinType.LEFT); //left

        if (search.getId() != null && !search.getId().isEmpty() && search.getId().length() > 5) {
            p.getExpressions().add(cb.equal(samplings.get("year"), search.getYear()));
            p.getExpressions().add(cb.equal(samplings.get("sequenceId"), search.getSeq()));
            p.getExpressions().add(cb.equal(root.get("sampleLetter"), search.getSampleLetter()));
        }

        if (search.getBuildDate() != null) {
            p.getExpressions().add(cb.equal(samplings.get("buildDate"), search.getBuildDate()));
        }

        if (search.getColorsId() != null) {
            p.getExpressions().add(cb.equal(colors.get("id"), search.getColorsId()));
        }

        if (search.getDimensionsId() != null) {
            p.getExpressions().add(cb.equal(dimension.get("id"), search.getDimensionsId()));
        }

        if (search.getMachinesId() != null) {
            p.getExpressions().add(cb.equal(machine.get("id"), search.getMachinesId()));
        }

        if (search.getProductTypesId() != null) {
            p.getExpressions().add(cb.equal(productType.get("id"), search.getProductTypesId()));
        }

        if (search.getProductsId() != null) {
            p.getExpressions().add(cb.equal(product.get("id"), search.getProductsId()));
        }

        if (search.getSuppliersId() != null) {
            p.getExpressions().add(cb.equal(supp.get("id"), search.getSuppliersId()));
        }

        if (search.getBuildStartDate() != null) {
            p.getExpressions().add(cb.greaterThanOrEqualTo(samplings.get("buildDate"), search.getBuildStartDate()));
        }

        if (search.getBuildEndDate() != null) {
            p.getExpressions().add(cb.lessThanOrEqualTo(samplings.get("buildDate"), search.getBuildEndDate()));
        }

        if (search.isPassing()) {
            //   p.getExpressions().add(cb.equal(root.get("passing"), search.isPassing()))
        }

        if (search.isRetained()) {
            //   p.getExpressions().add(cb.equal(root.get("passing"), search.isPassing()))
        }

        if (search.getPosition() != null) {
            p.getExpressions().add(cb.equal(root.get("position"), search.getPosition()));
        }

        return p;

    };

    return this.findAll(specification, page);
}

大多数对象使用延迟加载 是否可以在查询中获取它们...以避免对每一行进行多次查询?

0 个答案:

没有答案