title: JPA dynamically EntityGraph and Subgraphs
body: <p>I created an util method:</p>
  
  <pre><code>public &lt;T&gt; Optional&lt;T&gt; fetch(Class&lt;T&gt; clazz, Object id, String... relations) {
      EntityGraph&lt;T&gt; graph = entityManager.createEntityGraph(clazz);
      Stream.of(relations).forEach(graph::addSubgraph);
      return Optional.ofNullable(entityManager.find(clazz, id, Collections.singletonMap("javax.persistence.loadgraph", graph)));
  }
  </code></pre>
  
  <p>So if for example if <code>User</code> has lazy <code>orders</code> and <code>wallets</code>, I can do this:</p>
  
  <pre><code>Optional&lt;User&gt; user = fetch(User.class, 1, "orders", "wallets");
  </code></pre>
  
  <p>But I don't know how to take <code>orders</code>'s or <code>wallets</code> lazy collections. It would be greate if I would call method like this:</p>
  
  <pre><code>Optional&lt;User&gt; user = fetch(User.class, 1, "orders", "orders.products", "wallet");
  </code></pre>
  
  <p>How can I extend the method to achieve this?</p>