0

As the title says, I'd like to know if it's possible to lookup the container managed EntityManger (or EntityManagerFactory or PersistenceContext) through JNDI in an enterprise application deployed on Websphere Application Server ver. 8.5 or 9.0 without explicit references via <persistence-context-ref> / <persistence-unit-ref> in web.xml.

I found that it's possible in JBoss via the

<property name="jboss.entity.manager.factory.jndi.name" value="persistence-units/JpaPersistence"/>

entry in persistence.xml, but couldn't find anything similar for Websphere.

dekkard
  • 6,121
  • 1
  • 16
  • 26

1 Answers1

1

I believe the JPA specification, section 7.2.1, has the information you are looking for:

Persistence context injection example:

    @PersistenceContext(name="persistence-units/JpaPersistence")
    EntityManager em;

JNDI lookup example:

    @Stateless
    @PersistenceContext(name="persistence-units/JpaPersistence")
    public class MySessionBean implements MyInterface {
        @Resource SessionContext ctx;
        public void doSomething() {
            EntityManager em = (EntityManager)ctx.lookup("persistence-units/JpaPersistence");
            ...
        }
    }

I would recommend just having the container inject the reference using @PersistenceContext instead of looking up the resource manually

Will Dazey
  • 253
  • 2
  • 13