如何使用 JPA 规范中的列表

How to work with lists in JPA Specifications

我一直在努力使用 JPA 规范,但我不知道如何转换此查询:

select employees.* from employees
left join qualification_assignments on employees.qualification_profile_id =qualification_assignments.qualificationProfile_id
where qualification_id in <some list of strings>

我已经能够转换仅包含普通谓词的简单查询。但是,似乎没有任何关于更高级用例的文档。如果有人能指出正确的方向,我将不胜感激。

我的尝试看起来像这样导致各种异常:

Join<Employee, QualificationProfile> assignmentJoin = root.join("qualificationProfile");

return criteriaQuery.where(criteriaBuilder.in(assignmentJoin.get("qualificationAssignments"))
           .in(searchCriteria.getQualificationIds())).getGroupRestriction();

以下内容对我来说最有意义,但连接不起作用。

Join<Employee, QualificationAssignment> assignmentJoin = root.join("qualificationProfile");

return criteriaQuery.where(criteriaBuilder.in(assignmentJoin.get("qualificationId"))
           .in(searchCriteria.getQualificationIds())).getGroupRestriction();

数据模型如下所示:

https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/criteria-api-left-join-on-condition.html

也许有帮助。您可以生成 Meta class-es,例如 Employee_.

Root<Employee> employee = query.from(Employee.class);
ListJoin<Employee, QualificationProfile> profiles = employee.join(Employee_.qualificationProfile, JoinType.LEFT);
profiles.on(criteriaBuilder.in(profiles.get(QualificationProfile_.qualificationAssignments), searchCriteria.getQualificationIds()));

感谢 Lazlo Toth 的帮助,我走上了正轨。非常感谢。

最终规格如下:

Join<Employee, QualificationAssignment> profiles = root
                .join("qualificationProfile", JoinType.LEFT)
                .join("qualificationAssignments");
        
profiles.on(profiles.get("qualification").get("id")
                .in(new HashSet<>(searchCriteria.getQualificationIds())));

return profiles.isNotNull();

既然我有了一个有效的解决方案,我将仔细研究元 类。我发现 https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/criteria-api-in-predicate.html 很有帮助。