查询 parent 的集合 - Hibernate 规范

Query on parent's collection - Hibernate Specification

我用 Hibernate 创建了三个表。 Product是parent while barcodes是collection end price是child(一对多)的产品。

@NotBlank
private String ref;    

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "products_barcodes")
@Fetch(FetchMode.SELECT)
private List<String> barcodes;


@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<PriceEntity> prices;

我正在尝试从 child(价格)开始查询。我能够查询字符串,但现在我想查询集合的元素。

     Specifications<PriceEntity> specifications = where(hasTenant(tid));

     if (isNotBlank(ref)) {
         specifications = specifications.and(hasRef(ref));
     }

     if (isNotBlank(barcode)) {
         specifications = specifications.and(hasBarcode(barcode));
     }


     /*********************************/


    public static Specification<PriceEntity> hasRef(final String ref) {

        return new Specification<PriceEntity>() {

            @Override
            public Predicate toPredicate(Root<PriceEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.equal(root.<PriceEntity>get("parent").get("ref"), ref);
            }
        };
    }

public static Specification<PriceEntity> hasBarcode(final String barcode) {

    return new Specification<PriceEntity>() {

        @Override
        public Predicate toPredicate(Root<PriceEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            return criteriaBuilder.equal(root.<PriceEntity>get("parent").get("barcodes"), barcode);
        }
    };
}

您将如何编写规范?上面的那个不起作用,我在运行时得到这个异常:

"IllegalArgumentException: Parameter value [8003921360408] did not match expected type [java.util.Collection (n/a)]"

谢谢

Thomas 的评论解决了问题。

对于集合,应该使用 criteriaBuilder.isMember