Question

I'm using IBM RAD to develop some JPA entities, and from those, corresponding JPA Manager Beans. Manager beans (which are generated by RAD) have the following member:

@PersistenceUnit
private EntityManagerFactory emf;

I'm not sure how to correclty instantiate (or get a reference to) this manager bean from a stateless EJB (3.0), so I've added a constructor to manager bean where I can pass EntityManagerFactory instance to it. I get a reference to EntityManagerFactory in an EJB by using `@PersistenceUnit' annotation like so:

@PersistenceUnit
private EntityManagerFactory _entityManagerFactory;

This seems rather unnecessary and I believe there must be a way to tell the container (Websphere 7.0 in my case) to 'bootstrap' this for me somehow, so that I get a reference to JPA manager bean right away. Is it?

Update: It seems I haven't bean elaborate enough. Sorry for that.

  • There are three objects involved: JPA Entity, JPA Entity Manager and a Stateless EJB.
  • JPA Entity Manager class (not an EJB) is created by RAD and has convinience methods (named queries) on it. It also has defined member @PersistenceUnit private EntityManagerFactory emf. I know I can use EntityManager directly, but I'd like to use MyEntityManager for it's convinience methods.
  • I can get a reference to EntityManager or EntityManagerFactory in a stateless EJB by using annotations described above (and also like Bozho suggests)
  • I would like to get a reference to JPA Entity Manager in a stateless EJB. If I 'new' it up (new MyEntityManager()), the emf field is null. The workaround is to declare @PersistenceUnit field in an EJB and then pass it to JPA Entity Manager and use that.
  • Come to think of it, maybe I could declare JPA Entity Manager as an EJB and be done with it... (although this again seems unnecessary). I thought there is a method along the lines PersistenceContext.getJpaManager(MyEntityManager.class) which I am perhaps missing.
Was it helpful?

Solution

@PersistenceContext
private EntityManager em;

It seems you have your custom bean that you want to inject in other beans (it needs a local interface at least):

@EJB
private MyEntityManager em;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top