The following two more methods are available for using the JNDI to look up EntityManagerFactory from the application:
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:
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:
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 |
An example of using @PersistenceUnit to look up EntityManagerFactory from InitialContext is as follows:
@Stateless |
When you use the DD to define the EntityManagerFactory references, define the <persistence-unit-ref> tag of the DD:
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:
... |