为什么有时需要施放?
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);
答案 0 :(得分:0)
当我想要获得两者的好处时,我必须将Fetch
投射到Join
。
Fetch
允许在注释不起作用时急切加载,请参阅https://stackoverflow.com/a/25631785/2202712 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)
),则可以添加where
或on
表达式,以限制Collection
打破JPA Java模型的假设。>
@*ToOne
联接没有问题,因此某些JPA实现允许将Fetch
强制转换为Join
,前提是您了解自己在做什么))
其他阅读内容: