Title: persistence-unit-ref Both lookup and injection of an EntityManagerFactory can be configured via the @PersistenceUnit annotation or in xml. Annotations and xml have equal function in both lookup and injection. # Injection and Lookup ## Via annotation package org.superbiz; import javax.persistence.PersistenceUnit; import javax.persistence.EntityManagerFactory; import javax.ejb.Stateless; import javax.naming.InitialUnit; @Stateless public class MyBean implements MyInterface { @PersistenceUnit(unitName = "bar-unit") private EntityManagerFactory myBarEntityManagerFactory; public void someBusinessMethod() throws Exception { if (myBarEntityManagerFactory == null) throw new NullPointerException("myBarEntityManagerFactory not injected"); // Both can be looked up from JNDI as well InitialContext unit = new InitialContext(); EntityManagerFactory barEntityManagerFactory = (EntityManagerFactory) context.lookup("java:comp/env/org.superbiz.MyBean/myBarEntityManagerFactory"); } } ## Via xml The above @PersistenceUnit annotation usage is 100% equivalent to the following xml. org.superbiz.calculator.MyBean/myBarEntityManagerFactory bar-unit Transaction org.superbiz.calculator.MyBean myBarEntityManagerFactory # Lookup only ## Via annotation package org.superbiz; import javax.persistence.PersistenceUnit; import javax.persistence.EntityManagerFactory; import javax.ejb.Stateless; import javax.naming.InitialUnit; @Stateless @PersistenceUnit(name = "myFooEntityManagerFactory", unitName = "foo-unit") public class MyBean implements MyInterface { public void someBusinessMethod() throws Exception { InitialContext context = new InitialContext(); EntityManagerFactory fooEntityManagerFactory = (EntityManagerFactory) context.lookup("java:comp/env/myFooEntityManagerFactory"); } } # Via xml The above @PersistenceUnit annotation usage is 100% equivalent to the following xml. myFooEntityManagerFactory foo-unit Transaction