Programming the Annotated EJB 3.0 Class 5-7
An entity is a persistent object that represents datastore records; typically an instance of an entity represents a single row of a database table. Entities make it easy to query
and update information in a persistent store from within another Java EE component, such as a session bean. A Person entity, for example, might include name, address,
and age fields, each of which correspond to the columns of a table in a database. Using an javax.persistence.EntityManager object to access and manage the
entities, you can easily retrieve a Person record, based on either their unique id or by using a SQL query, and then change the information and automatically commit the
information to the underlying datastore.
The following sections describe the typical programming tasks you perform in your session bean to interact with entities:
■
Section 5.3.4.1, Injecting Persistence Context Using Metadata Annotations
■
Section 5.3.4.2, Finding an Entity Using the EntityManager API
■
Section 5.3.4.3, Creating and Updating an Entity Using EntityManager
5.3.4.1 Injecting Persistence Context Using Metadata Annotations
In your session bean, use the following metadata annotations inject entity information into a variable:
■
javax.persistence.PersistenceContext—Injects a persistence context into a variable of data type javax.persistence.EntityManager. A
persistence context is simply a set of entities such that, for any persistent identity, there is a unique entity instance. The persistence.xml file defines and names
the persistence contexts available to a session bean.
■
javax.persistence.PersistenceContexts—Specifies a set of multiple persistence contexts.
■
javax.persistence.PersistenceUnit—Injects a persistence context into a variable of data type javax.persistence.EntityManagerFactory.
■
javax.persistence.PersistenceUnits—Specifies a set of multiple persistence contexts.
The PersistenceContext and PersistenceUnit annotations perform a similar function: inject persistence context information into a variable; the main
difference is the data type of the instance into which you inject the information. If you prefer to have full control over the life cycle of the EntityManager in your session
bean, then use PersistenceUnit to inject into an EntityManagerFactory instance, and then write the code to manually create an EntityManager and later
destroy when you are done, to release resources. If you prefer that the EJB container manage the life cycle of the EntityManager, then use the PersistenceContext
annotation to inject directly into an EntityManager.
The following example shows how to inject a persistence context into the variable em of data type EntityManager; relevant code is shown in bold:
package examples; import javax.ejb.Stateless;
Note: It is assumed in this section that you have already
programmed the entity, as well as configured the database resources that support the entity. For details on that topic, see Java Persistence
API in the Oracle Kodo documentation.
5-8 Programming Enterprise JavaBeans, Version 3.0, for Oracle WebLogic Server
import javax.persistence.PersistenceContext; import javax.persistence.EntityManager;
Stateless public class ServiceBean
implements Service
{ PersistenceContext private EntityManager em;
...
5.3.4.2 Finding an Entity Using the EntityManager API