title: JPA Hibernate Lazy many-to-one fetch proxy
body: <p>I'm using JPA 2.1 and Hibernate 4.3.7</p>
  
  <p>I tried to tuned my app so I turn relationships to lazy and fetch only what I need them</p>
  
  <p>I have a problem with the many-to-one relationships, when turn to lazy when I load the entity again Hibernate replace the entity by a proxy even if I fetch the entity and this proxy is not working in the view part (JSF) of the application.
  The problem disapear when the many-to-one is in eager mode but hibernate perform one select more for each many-to-one even if I don't need them</p>
  
  <pre><code>@Entity
  public class Department {
      @Id
      private Integer id;
  
      //...
  }
  </code></pre>
  
  <p>1/</p>
  
  <pre><code>@Entity
  public class Employee {
      @Id
      private Integer id;
  
      @ManyToOne(fetch = FetchType.LAZY, optional = true)
      @JoinColumn(name = "id_department", referencedColumnName = "id")
      private Department department;
  
      //...
  }
  </code></pre>
  
  <p>the JPQL query:</p>
  
  <pre><code>SELECT e FROM Employee e LEFT JOIN FETCH e.department WHERE e.id=:id
  </code></pre>
  
  <p>=> one select query => faster but department is of type Department_$$_jvst3ac_5f (employee.getDepartment().getClass().getCanonicalName()) and this proxy doesn't work in the view part of the application</p>
  
  <p>2/</p>
  
  <pre><code>@Entity
  public class Employee {
      @Id
      private Integer id;
  
      @ManyToOne(fetch = FetchType.EAGER, optional = true)
      @JoinColumn(name = "id_department", referencedColumnName = "id")
      private Department department;
  
      //...
  }
  </code></pre>
  
  <p>the JPQL query:</p>
  
  <pre><code>SELECT e FROM Employee e WHERE e.id=:id
  </code></pre>
  
  <p>=> two selects => slower but department is loaded as Department and everything goes fine in the view part of the application</p>
  
  <p>The relation is unidirectional, Department have no references of emplyees</p>
  
  <p>Is this possible to have the department without proxy when using FETCH JOIN?</p>
  
  <hr>
  
  <p>After the response of Luiggi I will precised that the data are fetched with lazy many-to-one + fetch join. When I do a employee.getDepartment().toString() I have Department{ id=11, ...} but the class of this department is still Department_$$_jvst3ac_5f. For reason I don't know, the JSF/PrimeFaces selectOneMenu component don't work properly whith HibernateProxy even if the data are fetched</p>
  
  <p>I tried to use the Hibernate annotation @LazyToOne(LazyToOneOption.FALSE) in addition of @ManyToOne(fetch = FetchType.LAZY) but the result is similar of @ManyToOne(fetch = FetchType.EAGER) alone...</p>