JPA - 为什么要将Fetch转换为Join

时间:2017-05-05 18:03:30

标签: jpa jpa-criteria

为什么有时需要施放?

Join<X, Y> a = (Join) Fetch<X, Y> ...

例如:

Root<Person> personRoot = criteriaQuery.from(Person.class);

@SuppressWarnings("unchecked")
Join<Person, Loan> loanJoin = (Join) personRoot.fetch("loan", JoinType.INNER);
loanJoin.fetch("borrower", JoinType.LEFT);

不这样做的原因是什么:

Fetch<Person, Loan> fetchJoin = personRoot.fetch("loan", JoinType.INNER);
fetchJoin.fetch("borrower", JoinType.LEFT);

2 个答案:

答案 0 :(得分:0)

当我想要获得两者的好处时,我必须将Fetch投射到Join

例如,假设您希望将所有Employee与关于其部门和所在国家/地区的信息一起作为具有两个内部联接的单个选择查询。这可以通过为root.fetch(...)department各添加homeCountry来实现。如果您还希望根据各自国家的人口订购员工(请假设您愿意),您需要Join

Root<Employee> root = query.from(Employee.class);
root.fetch("department");  // <- for an eager join
Join<Employee,Country> joinCountry = (Join) root.fetch("homeCountry");  // <- for an eager join & orderBy
query.select(root).orderBy(builder.asc(joinCountry.get("population")));
Query<Employee> q = sessionFactory.getCurrentSession().createQuery(query);
List<Employee> employees = q.getResultList();

上面的代码向db

发出一个select *
select
    employee0_.emp_id as emp_id1_0_0_,
    department1_.department_id as depart1_2_1_,
    country2_.country_id as countr1_3_2_,
    employee0_.salary as salary2_0_0_,
    department1_.name as name3_0_0,
    country2_.continent as continent4_0_0
from
    employee employee0_
inner join
    department department1_
        on employee0_.department_id=department1_.department_id 
inner join
    country country2_
        on employee0_.country_id=country2_.country_id 
order by
    country2_.population asc

答案 1 :(得分:0)

肯定的答案在这里:How to properly express JPQL "join fetch" with "where" clause as JPA 2 CriteriaQuery?

基本上,JPA提供了@OneToMany,需要将所有相关实体提取到Collection。如果您允许对Fetch使用别名(如javax.persistence.criteria.Path.get(String)),则可以添加whereon表达式,以限制Collection打破JPA Java模型的假设。

@*ToOne联接没有问题,因此某些JPA实现允许将Fetch强制转换为Join,前提是您了解自己在做什么))

其他阅读内容: