title: JPA EntityGraph is not working; Always loading related collections
body: <p>I am trying to get named entity graphs to work properly.  Basically, I have a Customer who can have zero or more Addresses.  When I query to get a list of Customers, I want the customer's fields, but not the associated Addresses.  When I query for a particular Customer, I want its fields and all of the associated Addresses.  So, I've been trying to use named entity graphs because they seem to address this situation.  I created one to get basic information, and another that gets everything.  Unfortunately, when I use either graph, I still always get everything.  I am sure that I am making a simple mistake, but I cannot seem to find it.  I'd appreciate your help to figure out my error.  The relevant code follows...</p>
  
  <p>Thank you for your time and advise,
  Mike</p>
  
  <pre><code>/////////////////////////////////////////////////////////
  // ENTITIES
  /////////////////////////////////////////////////////////
  
  @Entity
  @Table(name = "customers")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")})
  
  @NamedEntityGraphs({
      @NamedEntityGraph(
          name="previewCustomerEntityGraph", 
          attributeNodes={
              @NamedAttributeNode("id"),
              @NamedAttributeNode("displayAs"),
              @NamedAttributeNode("rowVersion")
          }
      ),
      @NamedEntityGraph(
          name="fullCustomerEntityGraph", 
          attributeNodes={
              @NamedAttributeNode("id"),
              @NamedAttributeNode("displayAs"),
              @NamedAttributeNode("rowVersion"),
              @NamedAttributeNode(value = "addressCollection", subgraph = "addressCollection")
          },
          subgraphs = @NamedSubgraph(
              name = "addressCollection", 
              attributeNodes = {
                  @NamedAttributeNode("id"),
                  @NamedAttributeNode("displayAs"),
                  @NamedAttributeNode("rowVersion")
              }
          )
      )
  })
  public class Customer implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "Id")
      @XmlAttribute(required=true)
      private Long id;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 256)
      @Column(name = "DisplayAs")
      @XmlElement(required=true, nillable=false)
      private String displayAs;
  
      @Basic(optional = true)
      @Size(max = 256)
      @Column(name = "Name")
      @XmlElement(required=true, nillable=true)
      private String name;    
  
      @Basic(optional = false)
      @NotNull
      @XmlElement(required=true, nillable=false)
      @Column(name = "RowVersion")
      @Version
      private Timestamp rowVersion;
  
  
      @OneToMany(mappedBy = "customerId",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private Collection&lt;Address&gt; addressCollection;
  
  //@XmlTransient
  public Collection&lt;Address&gt; getAddressCollection() {
      return addressCollection;
  }
  
  public void setAddressCollection(Collection&lt;Address&gt; addressCollection) {
      this.addressCollection = addressCollection;
  }
  
      // Object methods have been removed to save space
  }
  
  
  @Entity
  @Table(name = "addresses")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a")})
  public class Address implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @XmlAttribute(required=true)
      @Column(name = "Id")
      private Long id;
  
      @Basic(optional = false)
      @NotNull
      @Column(name = "RowVersion")
      @Temporal(TemporalType.TIMESTAMP)
      @XmlElement(required=true, nillable=true)
      private Date rowVersion;
  
      @Basic(optional = false)
      @NotNull
      @Size(min = 1, max = 256)
      @Column(name = "DisplayAs")    
      @XmlElement(required=true, nillable=true)
      private String displayAs;
  
      @JoinColumn(name = "CustomerId", referencedColumnName = "Id")
      @ManyToOne
      private Customer customerId;
  
      // Object methods have been removed to save space
  }
  
  
  
  
  
  /////////////////////////////////////////////////////////
  // QUERIES
  /////////////////////////////////////////////////////////
  
      protected T find(Object id) {                   
          // Filter to only return entities belonging to the tenant.       
  
          EntityGraph eg = getEntityManager().getEntityGraph("fullCustomerEntityGraph");
  
          javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
          javax.persistence.criteria.CriteriaQuery&lt;T&gt; cq = cb.createQuery(entityClass);
          javax.persistence.criteria.Root&lt;T&gt; from = cq.from(entityClass);        
          javax.persistence.criteria.CriteriaQuery&lt;T&gt; select = cq.select(from);      
          cq.where(cb.and(
                  cb.equal(from.&lt;T&gt;get("id"), id)
          ));
  
          javax.persistence.TypedQuery&lt;T&gt; tq = getEntityManager().createQuery(select);
  
          tq.setHint("javax.persistence.fetchgraph", eg);
  
          return getSingleResultOrNull(tq);
      }
  
      protected List&lt;T&gt; findAll() {       
          // Filter to only return entities belonging to the tenant.
  
          EntityGraph eg = getEntityManager().getEntityGraph("previewCustomerEntityGraph");
  
          javax.persistence.criteria.CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
          javax.persistence.criteria.CriteriaQuery&lt;T&gt; cq = cb.createQuery(entityClass);
          javax.persistence.criteria.Root&lt;T&gt; from = cq.from(entityClass);        
          javax.persistence.criteria.CriteriaQuery&lt;T&gt; select = cq.select(from);       
          javax.persistence.TypedQuery&lt;T&gt; tq = getEntityManager().createQuery(select);
  
          tq.setHint("javax.persistence.fetchgraph", eg);
  
          return tq.getResultList();
      }
  
      protected T getSingleResultOrNull(javax.persistence.TypedQuery&lt;T&gt; query) {
          query.setMaxResults(1);
          List&lt;T&gt; list = query.getResultList();
          if (list.isEmpty()) {
              return null;
          }
  
          return list.get(0);
      }
  </code></pre>