Java Platform, Enterprise Edition (Java EE) 8 The Java EE Tutorial

## Using Entity Graphs in Query Operations

To specify entity graphs for both typed and untyped queries, call the `setHint` method on the query object and specify either `javax.persistence.loadgraph` or `javax.persistence.fetchgraph` as the property name and an `EntityGraph` instance as the value:

    EntityGraph<EmailMessage> eg = em.getEntityGraph("previewEmailEntityGraph");
    List<EmailMessage> messages = em.createNamedQuery("findAllEmailMessages")
            .setParameter("mailbox", "inbox")
            .setHint("javax.persistence.loadgraph", eg)
            .getResultList();

In this example, the `previewEmailEntityGraph` is used for the `findAllEmailMessages` named query.

Typed queries use the same technique:

    EntityGraph<EmailMessage> eg = em.getEntityGraph("previewEmailEntityGraph");

    CriteriaQuery<EmailMessage> cq = cb.createQuery(EmailMessage.class);
    Root<EmailMessage> message = cq.from(EmailMessage.class);
    TypedQuery<EmailMessage> q = em.createQuery(cq);
    q.setHint("javax.persistence.loadgraph", eg);
    List<EmailMessage> messages = q.getResultList();

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.