O'Reilly logo

Hands-On High Performance with Spring 5 by Dinesh Radadiya, Prashant Goswami, Pritesh Shah, Subhash Shah, Chintan Mehta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Join fetch in Criteria API

This approach is the same as JOIN FETCH in JPQL; however, this time, we are using the Criteria API of Hibernate. The following is an example of how to use JOIN FETCH in the Criteria API:

CriteriaBuilder criteriaBuilder =     getEntityManager().getCriteriaBuilder();    CriteriaQuery<?> query =     criteriaBuilder.createQuery(Account.class);    Root root = query.from(Account.class);    root.fetch("transactions", JoinType.INNER);    query.select(root);    query.where(criteriaBuilder.equal(root.get("accountId"),     accountId));    return (Account)this.getEntityManager().createQuery(query)   .getSingleResult();

This option has the same advantages and disadvantages as JPQL. Most of the time, when we write a query using the Criteria API, it is use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required