0

我有一堂课:

@Entity
public class Resume {
    private Long id;
    @Embedded
    private DesiredPositionAndSalary desiredPositionAndSalary;
}

和类:

@Embeddable
public class DesiredPositionAndSalary {
    @ManyToMany
    private Set<Specialization> specializations;
}

和类;)

@Entity
public class Specialization {
    private Long id;
}

现在我有一些我需要过滤的专业。例如,我需要选择具有程序员或经理等专业之一的所有简历。就像是

select * from resume r inner join resume_to_specialization rts on r.id = rts.id inner join specialization s on rts.spec_id in(1,2)

我如何在 Criteria api 中编写此查询?如果我错过了一些主要细节,我可以提供更多。

4

1 回答 1

0

好的,我用这个来处理它:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Resume> cq =cb.createQuery(Resume.class);
Root<Resume> root = cq.from(Resume.class);
cq.select(root);

Set<Specialization> filter = getFilter();

SetJoin<DesiredPositionAndSalary, Specialization> join = root.join(Resume_.desiredPositionAndSalary, JoinType.INNER).join(
            DesiredPositionAndSalary_.specializations, JoinType.INNER);

cq.where(cb.and(cq.getRestriction(), join.in(filter)));
cq.distinct(true);/*it is major, or we get duplicate of resume for every
                   specialization overlap with filter*/

List<Resume> result = em.createQuery(cq).getResultList();

.

于 2013-08-22T15:27:02.883 回答