使用CriteriaBuilder进行动态查询:一对多

时间:2019-08-15 10:51:30

标签: java jpa one-to-many criteria predicate

我想使用CriteriaBuilder和Predicate构建查询,该查询将仅返回具有“图像”的那些产品的列表。但是考虑到产品与Image @OneToMany之间存在关联这一事实,我不知道如何实现它。例如,使用JpaRepository,我知道可以通过以下方式构造此查询: List<Product> findAllByImagesIsNotNull()

但是我需要一个动态查询,所以据我所知,最好使用CriteriaBuilder。

所以,我有两个实体:

    @Entity
    @Table(name = "products")
    public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    long id;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "product_details_id")
    ProductDetails productDetails;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    Category category;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "lens_color_id")
    LensColor lensColor;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "frame_color_id")
    FrameColor frameColor;

    @OneToMany(fetch = FetchType.LAZY,
            cascade = CascadeType.ALL,
            orphanRemoval = true)
    List<Image> images = new ArrayList<>();

    @Column(unique = true)
    String productNumber;
}

    @Entity
    @Table(name = "images")    
    public class Image {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    String imageName;

    String imageType;

    boolean isMainImage;
}

我有两个元模型:

@StaticMetamodel(Product.class)
public abstract class Product_ {

    public static volatile SingularAttribute<Product, LensColor> lensColor;
    public static volatile ListAttribute<Product, Image> images;
    public static volatile SingularAttribute<Product, FrameColor> frameColor;
    public static volatile SingularAttribute<Product, Long> id;
    public static volatile SingularAttribute<Product, String> productNumber;
    public static volatile SingularAttribute<Product, ProductDetails> productDetails;
    public static volatile SingularAttribute<Product, Category> category;

}

@StaticMetamodel(Image.class)
public abstract class Image_ {

    public static volatile SingularAttribute<Image, String> imageName;
    public static volatile SingularAttribute<Image, Boolean> isMainImage;
    public static volatile SingularAttribute<Image, Long> id;
    public static volatile SingularAttribute<Image, String> imageType;

}

这是我的查询代码:

    public List<Product> findByCriteria(Integer minPrice, Integer maxPrice, List<LensColor> lensColor, List<FrameColor> frameColor) {
        return productDAO.findAll(new Specification<Product>() {
            @Override
            public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Join<Product, ProductDetails> productDetailsJoin = root.join(Product_.productDetails, JoinType.INNER);

                List<Predicate> predicates = new ArrayList<>();

// MY PREDICATE CODE FOR FIND ALL PRODUCTS WITH IMAGES IS NOT NULL

                if (!CollectionUtils.isEmpty(lensColor)) {
                    predicates.add(criteriaBuilder.and(root.get("lensColor").in(lensColor)));
                }
                if (!CollectionUtils.isEmpty(frameColor)) {
                    predicates.add(criteriaBuilder.and(root.get("frameColor").in(frameColor)));
                }
                if (minPrice != null && maxPrice != null) {
                    predicates.add(criteriaBuilder.between(productDetailsJoin.get(ProductDetails_.price), minPrice, maxPrice));
                }

                   return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

对于*多对多关系,请根据集合类型使用PluralJoin

Join<Product, Image> joinImages = root.joinList("images", JoinType.INNER);
predicates.add(cb.isNotNull(joinImages.get(Image_.id)));