5.7.2 Method of looking up EntityManagerFactory from the application

The following two more methods are available for using the JNDI to look up EntityManagerFactory from the application:

  1. Method of adding @PersistenceUnit in the class for looking up EntityManagerFactory and defining the EntityManagerFactory references
  2. Method of defining the <persistence-unit-ref> tag in the DD (web.xml) and defining the EntityManagerFactory references

However, you cannot specify the JPA definition using EJB 3.0 ejb-jar.xml with Application Server. Therefore, if you want to use the JPA in an EJB, use the method specified in 1.

The following subsections describe the methods:

Organization of this subsection
(1) Method of using @PersistenceUnit
(2) Method of using the <persistence-unit-ref> tag of the DD

(1) Method of using @PersistenceUnit

When you use @PersistenceUnit to define the EntityManagerFactory references, you add @PersistenceUnit in the class that executes lookup. The attributes that can be specified in @PersistenceUnit are as follows:

(a) name attribute

In the name attribute, you specify the lookup name with which the application code will look up EntityManagerFactory. The specified lookup name is the relative path from java:comp/env. The lookup name of EntityManagerFactory is not required, but the JPA specifications recommend that the name be set up under java:comp/env/persistence.

For @PersistenceUnit, the same attributes are used that are used as the other attributes in 5.7.1 Method of injecting EntityManagerFactory in the application. Note that to add multiple @PersistenceUnit in one class, you add @PersistenceUnits in the class and specify the array of @PersistenceUnit as the value attribute. An example of using @PersistenceUnit to look up EntityManagerFactory from SessionContext is as follows:

@Stateless
@PersistenceUnit(name="persistence/InventoryAppDB")
public class InventoryManagerBean implements InventoryManager {
@Resource SessionContext ctx;
public void updateInventory(...) {
...
EntityManagerFactory emf = (EntityManagerFactory)
 ctx.lookup("persistence/InventoryAppDB");
EntityManager em = emf.createEntityManager();
...
}
}

An example of using @PersistenceUnit to look up EntityManagerFactory from InitialContext is as follows:

@Stateless
@PersistenceUnit(name="persistence/InventoryAppDB")
public class InventoryManagerBean implements InventoryManager {
public void updateInventory(...) {
Context initCtx = new InitialContext();
EntityManagerFactory emf = (EntityManagerFactory)
 initCtx.lookup("java:comp/env/persistence/InventoryAppDB");
EntityManager em = emf.createEntityManager();
...
}
}

(2) Method of using the <persistence-unit-ref> tag of the DD

When you use the DD to define the EntityManagerFactory references, define the <persistence-unit-ref> tag of the DD:

(a) <persistence-unit-ref-name> tag

In the <persistence-unit-ref-name> tag, specify the lookup name with which the application code will look up EntityManagerFactory. The specified lookup name is the relative path from java:comp/env. The lookup name of EntityManagerFactory is not mandatory, but the JPA specifications recommend that the name be set under java:comp/env/persistence.

For the <persistence-unit-ref> tag, the same tags are used that are used as the other tags in 5.7.1 Method of injecting EntityManagerFactory in the application. However, when EntityManagerFactory is obtained with the JNDI lookup, you cannot specify <injection-target> tag. An example of defining <persistence-unit-ref> in web.xml is as follows:

...
<web-app>
...
<servlet>
<display-name>InventoryManagerServlet</display-name>
<servlet-name>InventoryManagerServlet</servlet-name>
<servlet-class>com.hitachi.InventoryManagerServlet</servlet-class>
</servlet>
...
<persistence-unit-ref>
<description>
 Persistence unit for the inventory management application.
</description>
<persistence-unit-ref-name>
 persistence/InventoryAppDB
</persistence-unit-ref-name>
<persistence-unit-name>InventoryManagement</persistence-unit-name>
</persistence-unit-ref>
...
</web-app>
...