title: JPA EntityGraph with different views using Spring
body: <p>I have a Spring application. After login, I am invoking getUserByEmail() method.</p>
  
  <p>I only need user and role data. Based on role I am going to display different views, Each view has different data and requiring different child entity of User.</p>
  
  <p>Seems like I have to call getUserByEmail() with different child entity.</p>
  
  <p>This is my partial code involving Entities:</p>
  
  <pre><code>EntityGraph(value = "withAddresses", type = EntityGraphType.FETCH)
  public class User{
    public firstName;
    public lastName;
  
    @OneToMany(fetch = FetchType.LAZY)
    public List&lt;Address&gt; addresses
  
    @OneToMany(fetch = FetchType.LAZY)
    public List&lt;Order&gt; orders;             
   }
  
   public userRepository extend jPaRepository(User.class,Long){
    @EntityGraph(name="withAdresses")
    getUserByEmail(String email)
  
    /* if possible */
    @EntityGraph(name="withOrder")
    getUserByEmail(String email)
   }
  </code></pre>
  
  <ol>
  <li><p>Is it possible to have two graph of User objects with same query name?
  Because I need different data for different views.</p></li>
  <li><p>Also, when switching to a new view (new call in spring controller), transaction from previous view will be closed already and I have to make new call to have different data with user. I don't understand how fetch lazy is helpful if you are not in same transaction service method, unless I am not missing something.</p></li>
  </ol>
  
  <p>For example if I need order data in "orderWiew.html" lazy load of order is not going to help I have to make another full call to same user data and additional Order data</p>