gpt4 book ai didi

jpa - 标准 API 关联

转载 作者:行者123 更新时间:2023-12-04 18:10:32 27 4
gpt4 key购买 nike

我一直在谷歌搜索,但不明白调用 javax.persistence.criteria.Subquery 和 Criteria API 的方法相关的结果是什么。

http://www.objectdb.com/api/java/jpa/criteria/Subquery/correlate_CollectionJoin_

这来自 Pro JPA2 Mastering the Java Persistence API 一书。

When creating the criteria API query definition for this query, we must correlate the employees attribute of Project and then join it to the direct reports in order to calculate the average salary. This example also demonstrates the use of the type() method of the Path interface in order to do a polymorphic comparison of types:


CriteriaQuery<Project> c = cb.createQuery(Project.class);
Root<Project> project = c.from(Project.class);
Join<Project,Employee> emp = project.join("employees");
Subquery<Number> sq = c.subquery(Number.class);
Join<Project,Employee> sqEmp = sq.correlate(emp);
Join<Employee,Employee> directs = sqEmp.join("directs");
c.select(project)
.where(cb.equal(project.type(), DesignProject.class),
cb.isNotEmpty(emp.<Collection>get("directs")),
cb.ge(sq, cb.parameter(Number.class, "value")));

这条线有什么作用?
加入 sqEmp = sq.correlate(emp);

最佳答案

它使您可以访问主查询引用的员工,以便您可以在子查询中使用它及其表

关于jpa - 标准 API 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15272235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com