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 departments 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.
B
B
B
answer : B
Observe that the query uses LEFT JOIN FETCH.
This forces the persistence provider to populate the employees collection with Department objects associated with the Department .
Therefore, when the code tries to access the employees objects later, no database access is required.
Correct answer is B.
The JOIN FETCH expression is not a regular JOIN and it does not define a JOIN variable. Its only purpose is specifying related objects that should be fetched from the database with the query results on the same round trip.