我有 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;
}
我想使用条件生成器和谓词按锡名搜索组织
对于搜索,我想要谓词列表
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
}
这应该适用于您的情况
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);