An application has two entities, Department and Employee, and there is a one-to-many relationship between them. The application has the following query:
SELECT d
FROM Department d LEFT JOIN FETCH d. employees
WHERE d.name = : name
After receiving the results of the query, the application accesses the returned department’s Employee entities stored in the Department.employees collection-valued attribute.
All caching has been turned off in the application.
Which statement is true?
A. The database will be accessed once during the query execution phase, and once for each Employee entity in Department – employees.
B. The database will be accessed once during the query execution phase ONLY.
C. The database will be accessed once during the query execution phase, and once when the department.employees collection-valued attribute is used.
D. The database will be accessed once during the query execution phase, once when the Department.
Employees collection-valued attribute is used, and once for each employee entity in the Department.employees.
Correct Answer: B
Explanation/Reference:
Explanation:
An important use case for LEFT JOIN is in enabling the prefetching of related data items as a side effect of a query. This is accomplished by specifying the LEFT JOIN as a FETCH JOIN.
10.2.3.5.3. JPQL Fetch Joins
A FETCH JOIN enables the fetching of an association as a side effect of the execution of a query. A FETCH JOIN is specified over an entity and its related entities. The syntax for a fetch join is fetch_join ::= [ LEFT [OUTER] | INNER ] JOIN FETCH join_association_path_expression The association referenced by the right side of the FETCH JOIN clause must be an association that belongs to an entity that is returned as a result of the query. It is not permitted to specify an identification variable for the entities referenced by the right side of the FETCH JOIN clause, and hence references to the implicitly fetched entities cannot appear elsewhere in the query. The following query returns a set of magazines. As a side effect, the associated articles for those magazines are also retrieved, even though they are not part of the explicit query result. The persistent fields or properties of the articles that are eagerly fetched are fully initialized. The initialization of the relationship properties of the articles that are retrieved is determined by the metadata for the Article entity class.
SELECT mag FROM Magazine mag LEFT JOIN FETCH mag.articles WHERE mag.id = 1
A fetch join has the same join semantics as the corresponding inner or outer join, except that the related objects specified on the right-hand side of the join operation are not returned in the query result or otherwise referenced in the query. Hence, for example, if magazine id 1 has five articles, the above query returns five references to the magazine 1 entity.
References:
Download Printable PDF. VALID exam to help you PASS.
|
|