5.6.1 Method of injecting EntityManager in the application

The following two more methods are available for injecting EntityManager in the application fields and in the setter method:

  1. Method of adding @PersistenceContext in the field or method at the inject destination
  2. Method of specifying definitions in the <persistence-context-ref> tag of a DD (web.xml)

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 point 1.

The following is a description of the methods:

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

(1) Method of using @PersistenceContext

When you use @PersistenceContext to inject EntityManager, you add @PersistenceContext in the field and setter method where EntityManager is to be injected. The attributes that can be specified in @PersistenceContext are as follows:

(a) unitName attribute

In the unitName attribute, you specify the name of the persistence unit defined in persistence.xml. However, when the persistence unit to be used can be uniquely identified, such as when only one persistence unit is defined in the EJB-JAR and WAR or EAR file, you can omit the unitName attribute. For details on the persistence unit used when the unitName attribute is omitted, see 5.11.2 Reference scope of the persistence unit name.

(b) type attribute

In the type attribute, you specify the life cycle type of the persistence context. You can specify PersistenceContextType.TRANSACTION or PersistenceContextType.EXTENDED.

When the type attribute is omitted, the default value is PersistenceContextType.TRANSACTION.

(c) properties attribute

In the properties attribute, you can specify the properties for the JPA provider used for setting up a persistence unit. The properties specified here are passed to the JPA provider when EntityManager is obtained from the JPA provider.

(d) name attribute

When you use injection, normally you need not specify the name attribute, but if specified, EntityManager is registered in the JNDI Namespace (java:comp/env) with the name specified in the name attribute. An example of using @PersistenceContext to inject EntityManager is as follows:

@Stateless
public class InventoryManagerBean implements InventoryManager {
@PersistenceContext(unitName="myUnit")
private EntityManager em;
...
}

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

When you use a DD to inject EntityManager, you define the following tags in the <persistence-context-ref> tag of the DD:

(a) <description> tag

In the <description> tag, the user can freely code the explanation for the EntityManager references to be defined. Even if this tag is specified, the specified contents do not affect the operations of the application. You can also omit this tag.

(b) <persistence-context-ref-name> tag

In the <persistence-context-ref-name> tag, specify the name with which EntityManager is registered in the JNDI Namespace. The specified name is the relative path from java:comp/env. The JNDI registration name of EntityManager is not mandatory, but JPA specifications recommend that the name be set under java:comp/env/persistence.

(c) <persistence-unit-name> tag

In the <persistence-unit-name> tag, you specify the name of the persistence unit defined in persistence.xml. However, when the persistence unit to be used can be uniquely identified, such as when only one persistence unit is defined in the EJB-JAR and WAR or EAR, you can omit the <persistence-unit-name> tag. For details on the persistence unit used when the <persistence-unit-name> tag is omitted, see 5.11.2 Reference scope of the persistence unit name.

(d) <persistence-context-type> tag

In the <persistence-context-type> tag, you specify the life cycle type of the persistence context. You specify Transaction or Extended.

When the <persistence-context-type> tag is omitted, the default value is Transaction.

(e) <persistence-property> tag

In the <persistence-property> tag, you can specify the properties for the JPA provider used for setting up the persistence unit. The properties specified here are passed to the JPA provider when EntityManager factory is obtained from the JPA provider. You can omit this tag.

(f) <injection-target> tag

In the <injection-target-class> tag of the <injection-target> tag, you specify the inject destination class. In the <injection-target-name> tag of the <injection-target> tag, you specify the field name or setter method name at the inject destination. An example of defining the <persistence-context-ref> tag in web.xml and injecting EntityManager 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-context-ref>
<description>
 Persistence context for the inventory management application.
</description>
<persistence-context-ref-name>persistence/InventoryAppMgr
</persistence-context-ref-name>
<persistence-unit-name>InventoryManagement</persistence-unit-name>
<persistence-context-type>Transaction</persistence-context-type>
<injection-target>
 <injection-target-class>
 com.hitachi.InventoryManagerServlet
 </injection-target-class>
 <injection-target-name>em</injection-target-name>
</injection-target>
</persistence-context-ref>
...
</web-app>
...