title: @ManyToOne(fetch=FetchType.LAZY) lazy loading not working
body: <p>I am working with JPA 2.1 (EclipseLink 2.5.1) and JBoss 7.1.</p>
  
  <p>I've define very simple JPA entities:</p>
  
  <pre><code>@Entity
  @Table(name="APLICACIONES_TB")
  public class Aplicacion implements Serializable {
  
    @Id
    @Column(name="COD_APLICACION_V")
    private long codAplicacionV;
  
    @Column(name="APLICACION_V")
    private String aplicacionV;
  
    @OneToMany(mappedBy="aplicacion")
    private Collection&lt;Prestacion&gt; prestaciones;
  
    ... getters and setters
  }
  
  @Entity
  @Table(name="PRESTACIONES_TB")
  public class Prestacion implements Serializable {
  
    @Id
    @Column(name="COD_PRESTACIONES_V")
    private String codPrestacionesV;
  
    @Column(name="DESCRIPCION_V")
    private String descripcionV;
  
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "COD_APLICACION_V")    
    private Aplicacion aplicacion;
  
    ... getters and setters ...
  }
  </code></pre>
  
  <p>I have developed a staless EJB that executes a query to obtain some "Aplicacion" entities.</p>
  
  <pre><code>@Stateless
  @LocalBean
  public class DocuEJB implements DocuEJBLocal
  {
    @PersistenceContext(name="DocuEjb", type=PersistenceContextType.TRANSACTION)
    private EntityManager em;
  
    public Prestacion getResult(String name)
    {
      return em.createNamedQuery("ExampleQueryName", Prestacion.class).getSingleResult();
    }
  }
  </code></pre>
  
  <p>Because I'm working with JSF 2.1 the EJB is being injected in a managed bean:</p>
  
  <pre><code>@ManagedBean(name = "ManagedBean")
  @RequestScoped
  public class ManagedBean
  {   
    @EJB DocuEJB docuEjb;
  
    public String doSomething()
    {
       Prestacion entity = docuEjb.getResult("egesr");
  
       if (entity != null)
       {
          // It should return null because 'entity' should be detached
          Aplicacion app = entity.getAplicacion();
  
          // but 'app' entity is not null, ¿why not?
          System.out.println (app.getCodAplicacionV());
       }
    }
  }
  </code></pre>
  
  <p>Lazy loading is not working even when lazy loading has been defined for 'aplicacion' field on 'Prestacion' entity. The code posted before should return a NullPointerException in the next line:</p>
  
  <pre><code>System.out.println (app.getCodAplicacionV());
  </code></pre>
  
  <p>because 'app' entity is detached and lazy loading has been configured.</p>
  
  <p>Why is not working lazy loading?</p>
  
  <p>Thanks</p>