title: Using JPA entity graph with complex conditions
body: <p>I have a project built on Spring MVC + JPA + Hibernate. I am using entity graphs (JPA 2.1) for defining which data to fetch from the database, like in the example below.</p>
  
  <pre><code>EntityGraph&lt;Company&gt; entityGraph = entityManager.createEntityGraph(Company.class);
  entityGraph.addAttributeNodes("reviews");
  
  Map&lt;String, Object&gt; hints = new HashMap&lt;String, Object&gt;();
  hints.put("javax.persistence.loadgraph", entityGraph);
  
  Company company = entityManager.find(Company.class, companyId, hints);
  </code></pre>
  
  <p>My <code>Review</code> entity has an association with a <code>Company</code> entity (<code>ManyToOne</code>).</p>
  
  <p>Here I simply fetch a <code>Company</code> object with a populated <code>reviews</code> collection. This works well in scenarios like the above. But what if I want to fetch all or some of the reviews of a given company? That is, the <code>Review</code> objects that are associated with a company with a given ID. I want a <code>List&lt;Review&gt;</code> instead of a <code>Company</code> object with a <code>List&lt;Review&gt;</code>. This is just an example - basically I am looking for more flexibility than simply looking up an object based on a primary key. I can do it with HQL with no problem, but then I would have to write several similar queries depending on which data I need in a specific context.</p>
  
  <p>The <code>find</code> method on <code>javax.persistence.EntityManager</code> simply makes it possible to query for an object based on a primary key. But is it somehow possible to use entity graphs in more complicated scenarios, e.g. with Criteria objects or HQL queries? For example, looking up objects with other conditions than by primary key - perhaps even conditions on associations.</p>
  
  <p>I hope I made myself clear. Thanks in advance!</p>