mischu
mischu

Reputation: 43

two nested EJB Beans - only the first get an entiy manager injected

I've a stateful and a singelton EJB Bean.

The stateful bean uses the entity manager (injected) and calls the singelton bean. The singelton bean uses the entity manager (injected).

If i try to call the singelton bean from the stateful bean, the singelton bean doesn't get an entity manager injected.

Is it not possible to get an entity manager in both beans at the same time?

EJB Bean

@Singleton
@LocalBean
public class AllocationPlanController implements AllocationPlanControllerRemote {

@PersistenceContext
private EntityManager em;

EJB Bean two

@Stateful
@LocalBean
public class AllocationController implements AllocationControllerRemote {

@PersistenceContext
private EntityManager em;

private Allocation allocation;
private AllocationPlan allocationPlan;

AllocationPlanController allocationPlanController = new AllocationPlanController();

Upvotes: 3

Views: 479

Answers (2)

Tom Tresansky
Tom Tresansky

Reputation: 19877

Instead of creating a new AllocationPlanController via its constructor, try annotating it like so:

@EJB
AllocationPlanController allocationPlanController;

Then the container will inject that bean into your AllocationController, and since the container will inject the bean it has created, it will have already injected its depenedencies, so you will find a non-null value for em.

Upvotes: 0

Giorgos Dimtsas
Giorgos Dimtsas

Reputation: 12629

The EntityManager is not injected into the AllocationPlanController because you are "manually" creating the AllocationPlanController instance with it's constructor. You should inject the AllocationPlanController into the AllocationController bean and let the container managed it's lifecycle.

Upvotes: 3

Related Questions