3

I have a web module and an ejb module packaged into an EAR.

I have JSF backing beans successfully injecting Session EJBs from from the ejb module which are able to pull data from the DB, E.g.

@RequestScoped
public class CarController {

    @EJB
    private CarService carService;

To support Primefaces LazyDataModel, the session EJB exposes it's EntityManager to a LazyDataModel:

@PostConstruct
public void init() {
    MyLazyDataModel<Car> myLazyDataModel = new MyLazyDataModel<Car>();
    myLazyDataModel.setEntityManager(carService.getEntityManager());
    super.setMyLazyDataModel(myLazyDataModel);
}

This causes the follow exception:

java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
at    com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:132)
at    com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:173)
at   com.sun.enterprise.container.common.impl.EntityManagerWrapper.getCriteriaBuilder(EntityManagerWrapper.java:895)
at com.cars.web.controller.MyLazyDataModel.load(MyLazyDataModel.java:154)
at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:731)

Is this not working because the PersistenceContext is only a valid bean to pass around within the ejb module?

anger
  • 1,018
  • 1
  • 9
  • 25

1 Answers1

0

For the purpose of resource and / or EJB injection, working with JSF beans or CDI beans is equivalent.

I would avoid retrieving the EntityManager in two different ways (i.e. through PersistenceContext injection and through EntityManagerFactory) inside the same application. In particular the EntityManagerFactory instantiation is a heavyweight operation.

If you make MyLazyDataModel an anonymous inner class, it's much easier because you can use the injected DAO service directly, don't need to handle the EntityManager directly in the bean. You can see a concrete example in this answer.

Unrelated to the concrete problem, the best suited bean scope for a lazy datatable seems to be at least ViewScoped (that is only available for jsf beans).

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117