我有一个非常简单的实体产品,它有代码,名称和标签.标签存储在另一个带有product_id和tag列的表(product_tag)中.
我需要使用CriteriaQuery搜索带有某些标签的产品.举个例子,我想找到具有’fruit’和’red’标签的产品.
使用spring 4.1.x,spring-data-jpa 1.8和hibernate 4.2.x.
我的实体只是;
@Entity
@Table(name = "product",uniqueConstraints ={
@UniqueConstraint(columnNames = "code")
}
)
@NamedQueries({
@NamedQuery(name = "Product.findAll",query = "select p from Product p")
})
public class Product extends EntityWithId {
@Column(name = "code",length = 128)
private String code;
@Column(name = "name",length = 512)
protected String name;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name="product_tag",joinColumns=@JoinColumn(name="product_id"))
@Column(name="tag")
private Set
private void search() {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery
java.lang.IllegalArgumentException: Parameter value [fruit] did not match expected type [java.util.Set (n/a)]
我真的很想使用CriteriaQuery for ElementCollection和CollectionTable.
最佳答案
productTags映射到单独的表,因此您需要在查询中加入该表.
原文链接:https://www.f2er.com/spring/432655.html...
if (!StringUtils.isEmpty(tagsSearch.getValue())) {
//Util.parseCommaSeparated returns Set
注意product.join(“productTags”)而不是product.get(“productTags”)