Understanding how spring-data handles @EntityGraph
<p>(I made a <a href="https://github.com/ArnaudDenoyelle/sscce-entity-graph" rel="noreferrer">SSCCE</a> for this question.)</p>

<p>I have 2 simple entities : <code>Employee</code> and <code>Company</code>. <code>Employee</code> has a <code>@ManyToOne</code> relationship with <code>Company</code> with default fetch strategy (eager).</p>

<p>I want to be able to load the <code>Employee</code> without the <code>Company</code>without changing the fetch strategy defined in the <code>Employee</code> because I need to do that for only one use case.</p>

<p>JPA's entity graph seems to be intended for this purpose.</p>

<p>So I defined a <code>@NamedEntityGraph</code>on the class <code>Employee</code>:</p>

<pre><code>@Entity
@NamedEntityGraph(name = "employeeOnly")
public class Employee {

  @Id
  private Integer id;
  private String name;
  private String surname;
  @ManyToOne
  private Company company;

  //Getters &amp; Setters
</code></pre>

<p>And a <code>EmployeeRepository</code> like this : </p>

<pre><code>public interface EmployeeRepository extends CrudRepository&lt;Employee, Integer&gt; {

  @EntityGraph(value = "employeeOnly", type = EntityGraph.EntityGraphType.FETCH)
  List&lt;Employee&gt; findByCompanyId(Integer companyId);

}
</code></pre>

<p>Despite the use of <code>@EntityGraph</code>, I can see in the logs that the <code>Company</code> is still loaded by hibernate : </p>

<pre><code>2016-11-07 23:16:08.738 DEBUG 1029 --- [nio-8080-exec-2] org.hibernate.SQL                        : select employee0_.id as id1_1_, employee0_.company_id as company_4_1_, employee0_.name as name2_1_, employee0_.surname as surname3_1_ from employee employee0_ left outer join company company1_ on employee0_.company_id=company1_.id where company1_.id=?
2016-11-07 23:16:08.744 DEBUG 1029 --- [nio-8080-exec-2] org.hibernate.SQL                        : select company0_.id as id1_0_0_, company0_.name as name2_0_0_ from company company0_ where company0_.id=?
</code></pre>

<p>Why? How to avoid that?</p>