title: Spring Data JPA And NamedEntityGraphs
body: <p>currently I am wrestling with being able to fetch only the data I need. The findAll() method needs to fetch data dependant on where its getting called.
  I do not want to end up writing different methods for each entity graph.
  Also, I would avoid calling entitymanagers and forming the (repetitive) queries myself.
  Basicly I want to use the build in findAll method, but with the entity graph of my liking. Any chance?</p>
  
  <pre><code>@Entity
  @Table(name="complaints")
  @NamedEntityGraphs({
      @NamedEntityGraph(name="allJoinsButMessages", attributeNodes = {
              @NamedAttributeNode("customer"),
              @NamedAttributeNode("handling_employee"),
              @NamedAttributeNode("genre")
      }),
      @NamedEntityGraph(name="allJoins", attributeNodes = {
              @NamedAttributeNode("customer"),
              @NamedAttributeNode("handling_employee"),
              @NamedAttributeNode("genre"),
              @NamedAttributeNode("complaintMessages")
      }),
      @NamedEntityGraph(name="noJoins", attributeNodes = {
  
      })
  })
  public class Complaint implements Serializable{
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue
      private long id;
  
      private Timestamp date;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "customer")
      private User customer;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "handling_employee")
      private User handling_employee;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="genre")
      private Genre genre;
  
      private boolean closed;
  
      @OneToMany(mappedBy = "complaint", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      private List&lt;ComplaintMessage&gt; complaintMessages = new ArrayList&lt;ComplaintMessage&gt;();
  
  //getters and setters
  }
  </code></pre>
  
  <p>And my JPARepository</p>
  
  <pre><code>@Repository
  public interface ComplaintRepository extends JpaRepository&lt;Complaint, Long&gt;{
  
      List&lt;Complaint&gt; findByClosed(boolean closed);
  
      @EntityGraph(value = "allJoinsButMessages" , type=EntityGraphType.FETCH)
      @Override
      List&lt;Complaint&gt; findAll(Sort sort);
  }
  </code></pre>