FROM Employee emp JOIN emp.department dep
and
FROM Employee emp JOIN FETCH emp.department dep
Is there any difference between them? If yes, which one to use when? the difference is:
in the first query you are returning only the Employes for the Hibernate. In the second query, you are returning the Employes and all Departments associated.
If you don't usefetch
and the Departments continue to be returned, is because your mapping between Employee and Department (a@OneToMany
) are setted withFetchType.EAGER
. In this case, any HQL (withfetch
or not) query withFROM Employee
will bring all Departments. Remember that all mapping *ToOne (@ManyToOne
and@OneToOne
) are EAGER by default.
原文を参照:http://zccbbg.top/2017/05/31/difference-join-join-fetch-jpa/
Difference between JOIN and JOIN FETCH in JPA
1393 ワード
where to use a regular JOIN and where a JOIN FETCH? For example, if we have these two queries