Middle
Kas toimub JOIN FETCH operatsioon, kasutades laiska laadimist?
sobes.tech AI
Vastus AI-lt
The JOIN FETCH operation in JPA is used for eager loading of related entities, meaning it loads the related objects immediately along with the main query. The annotation or configuration of lazy loading (FetchType.LAZY) indicates that related data should only be loaded on demand.
If JOIN FETCH is used in the query, the related entities will be loaded immediately, regardless of what is specified in the lazy loading annotation. In other words, JOIN FETCH overrides the loading strategy and enforces eager loading.
Therefore, when using JOIN FETCH, the operation will be executed and the related data will be loaded immediately, despite the lazy loading set in the mapping.
Example:
String jpql = "SELECT p FROM Parent p JOIN FETCH p.children WHERE p.id = :id";
Parent parent = entityManager.createQuery(jpql, Parent.class)
.setParameter("id", 1L)
.getSingleResult();
// Children will be loaded immediately, even if in Parent.children FetchType.LAZY is set