我用Hibernate创建了三个表。Product是父产品,而条形码是一个集合端,是产品的一个子类(一对多)。
@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;我试图从孩子(Price)开始进行查询。我能够查询字符串,但现在我想查询集合的元素。
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)]"谢谢
发布于 2021-05-21 07:41:06
托马斯的评论解决了这个问题。
对于集合,应该使用criteriaBuilder.isMember。
https://stackoverflow.com/questions/67571570
复制相似问题