我有2个实体:
@Entity
public class Organization {
@Column(name = "name")
public String name;
@OneToMany(mappedBy = "organization")
public Collection<Tin> tin;
}
@Entity
public class Tin {
@Column(name = "name")
public String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ORGANIZATION_ID", nullable = false)
public Organization organization;
}
我想使用CriteriaBuilder和谓词
搜索Tin by Tin名称对于搜索,我想要有谓词列表
List<Predicate> getPredicates() {
List<Predicate> predicates = new ArrayList<Predicate>();
....
if (StringUtils.isNotBlank(orgSearchCriteria.getTaxIdNumber())) {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Tin> criteria = criteriaBuilder.createQuery(Tin.class);
Root<Tin> companyRoot = criteria.from( Tin.class );
predicates.add(
criteriaBuilder.equal(companyRoot.join("tin").get("name"), orgSearchCriteria.getName())
);
}
....
return predicates
}
答案 0 :(得分:0)
这应该适合您的情况
root = criteriaQuery.from(Organization.class);
Join<Organization, Set<Tin>> resource = root.join("tin");
criteriaQuery = criteriaQuery.select(root);
Predicate predicate = criteriaBuilder.equal(resource.get(NAME_COLUMN), "name");
criteriaQuery.where(predicate);
entityManager.createQuery(criteriaQuery);