2
votes

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

2
try to add @Transactional on doSomething()Xstian
If getAplicacion() is called in a detached object it is supposed to raise the infamous LazyInitializationException. It's not... so app is still attached.ivan.aguirre
@Xstian - I've tried what you suggested (transactional persistence context) but I've got the same result.Eduardo
@Ivan.aguirre - But it can not be attached as I'm using a transactional persistence context in the ejb so when the ejb method is finished, the persistence context is clear and all entities are detached. I've read this in all JPA booksEduardo
I've read in internet that lazy links works with EclipseLink provider even when entities are detached. In such a case, EclipseLink will simply take a read-only connection from connection pool, execute loading and then put the connection back to pool. I've read this in the next link: softaria.com/2011/07/26/eclipselink-versus-hibernate. But I would like to find this explanation in EclipseLink documentation.Eduardo

2 Answers

0
votes

Try to add @Transactional on doSomething(), I think that your transaction manager is not well configured.

You can see here the official spring documentation. In any case, can you add your spring configurations, so that we can better help you. :)

0
votes

I don't think the behavior your encounter is abnormal or your question should state it clearly:

  • EJB are by default transactional
  • Your JSF inject an EJB, with @EJB, and I guess JBoss can create a java reference and not a proxy
  • The entity is being managed because the transaction is not done, it will finish when doSomething ends.

Your entity is then loaded into the EntityManager, and lazy loading works because there is a context to it.

You would call em.evict(entity) with the result your are getting, this would probably fails because the entity would not be managed any more.