June 2018
Intermediate to advanced
408 pages
11h 23m
English
Normally, we call the findById method of DAO to fetch the outer or parent entity and then call the getter methods of associations. Doing so leads to n + 1 queries because the framework will execute additional queries for each association. Instead, we can write a JPQL query using the createQuery method of EntityManager. In this query, we can join our associated entity, which we want to fetch along with the outer entity by using JOIN FETCH. The following is an example of how to get JOIN FETCH entities:
Query query = getEntityManager().createQuery("SELECT a FROM Account AS a JOIN FETCH a.transactions WHERE a.accountId=:accountId", Account.class);query.setParameter("accountId", accountId);return (Account)query.getSingleResult(); ...Read now
Unlock full access