I am working with JPA 2.1 (EclipseLink 2.5.1) and JBoss 7.1.
I've define very simple JPA entities:
@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<Prestacion> 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 ...
}
I have developed a staless EJB that executes a query to obtain some "Aplicacion" entities.
@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();
}
}
Because I'm working with JSF 2.1 the EJB is being injected in a managed bean:
@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());
}
}
}
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:
System.out.println (app.getCodAplicacionV());
because 'app' entity is detached and lazy loading has been configured.
Why is not working lazy loading?
Thanks