我有两个实体:post和attributes
public class DbPost {
....
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "posts_attributes",
joinColumns = {@JoinColumn(name = "post_id")},
inverseJoinColumns = {@JoinColumn(name = "attribute_value_id")})
@Builder.Default
private Set<DbAttributeValue> attributeValues = new HashSet<>();
....
}
public class DbAttributeValue {
...
@Id
@Column(name = "attribute_value_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@PropertyDefinition
private Long id;
...
}
现在,如何从attributevalue id列表中获取所有包含attributevalue的帖子?
我已经试过了:
List<DbPost> findAllByAttributeValuesIdIn(Collection<Long> attributeValues_Id);
和
public Specification<DbPost> createPostJpaSpecification(List<Long> attributeValueIds) {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
if (attributeValueIds != null && attributeValueIds.size() > 0) {
predicates.add(
root.joinSet("attributeValues").get("id").in(attributeValueIds)
);
}
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
}
我做错什么了?
1条答案
按热度按时间p4tfgftt1#
试试这个