The following two more methods are available for injecting EntityManager in the application fields and in the setter method:
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:
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:
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.
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.
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.
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 |
When you use a DD to inject EntityManager, you define the following tags in the <persistence-context-ref> tag of the DD:
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.
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.
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.
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.
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.
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:
... |