Managing Entities
Entities are managed by the entity manager. The entity manager is represented by
javax.persistence.EntityManagerinstances. EachEntityManagerinstance is associated with a persistence context. A persistence context defines the scope under which particular entity instances are created, persisted, and removed.The Persistence Context
A persistence context is a set of managed entity instances that exist in a particular data store. The
EntityManagerinterface defines the methods that are used to interact with the persistence context.The EntityManager
The
EntityManagerAPI creates and removes persistent entity instances, finds entities by the entity's primary key, and allows queries to be run on entities.Container-Managed Entity Managers
With a container-managed entity manager, an
EntityManagerinstance's persistence context is automatically propagated by the container to all application components that use theEntityManagerinstance within a single Java Transaction Architecture (JTA) transaction.JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an
EntityManageris injected into the application components via thejavax.persistence.PersistenceContextannotation. The persistence context is automatically propagated with the current JTA transaction, andEntityManagerreferences that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references toEntityManagerinstances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.To obtain an
EntityManagerinstance, inject the entity manager into the application component:Application-Managed Entity Managers
With application-managed entity managers, on the other hand, the persistence context is not propagated to application components, and the lifecycle of
EntityManagerinstances is managed by the application.Application-managed entity managers are used when applications need to access a persistence context that is not propagated with the JTA transaction across
EntityManagerinstances in a particular persistence unit. In this case, eachEntityManagercreates a new, isolated persistence context. TheEntityManager, and its associated persistence context, is created and destroyed explicitly by the application.Applications create
EntityManagerinstances in this case by using thecreateEntityManagermethod ofjavax.persistence.EntityManagerFactory.To obtain an
EntityManagerinstance, you first must obtain anEntityManagerFactoryinstance by injecting it into the application component via thejavax.persistence.PersistenceUnitannotation:Then, obtain an
EntityManagerfrom theEntityManagerFactoryinstance:Finding Entities Using the EntityManager
The
EntityManager.findmethod is used to look up entities in the data store by the entity's primary key.@PersistenceContext EntityManager em; public void enterOrder(int custID, Order newOrder) { Customer cust = em.find(Customer.class, custID); cust.getOrders().add(newOrder); newOrder.setCustomer(cust); }Managing an Entity Instance's Lifecycle
You manage entity instances by invoking operations on the entity via an
EntityManagerinstance. Entity instances are in one of four states: new, managed, detached, or removed.New entity instances have no persistent identity and are not yet associated with a persistence context.
Managed entity instances have a persistent identity and are associated with a persistence context.
Detached entity instances have a persistent identify and are not currently associated with a persistence context.
Removed entity instances have a persistent identity, are associated with a persistent context, and are scheduled for removal from the data store.
Persisting Entity Instances
New entity instances become managed and persistent by invoking its
persistmethod. This means the entity's data is stored to the database when the transaction associated with thepersistoperation is completed. If the entity is already managed, thepersistoperation is ignored. Ifpersistis called on a removed entity instance, it becomes managed. If the entity is detached,persistwill throw anIllegalArgumentException, or the transaction commit will fail.@PersistenceContext EntityManager em; ... public LineItem createLineItem(Order order, Product product, int quantity) { LineItem li = new LineItem(order, product, quantity); order.getLineItems().add(li); em.persist(li); return li; }The
persistoperation is propagated to all entities related to the calling entity that have thecascadeelement set toALLorPERSISTin the relationship annotation.@OneToMany(cascade=ALL, mappedBy="order") public Collection<LineItem> getLineItems() { return lineItems; }Removing Entity Instances
Managed entity instances are removed by invoking its
removemethod, or by a cascadingremoveoperation invoked from related entities that have thecascade=REMOVEorcascade=ALLelements set in the relationship annotation. If theremovemethod is invoked on a new entity, theremoveoperation is ignored, although remove will cascade to related entities that have thecascadeelement set toREMOVEorALLin the relationship annotation. Ifremoveis invoked on a detached entity it will throw anIllegalArgumentException, or the transaction commit will fail. Ifremoveis invoked on an already removed entity, it will be ignored. The entity's data will be removed from the data store when the transaction is completed, or as a result of theflushoperation.public void removeOrder(Integer orderId) { try { Order order = em.find(Order.class, orderId); em.remove(order); }...In this example, all
LineItementities associated with the order are also removed, asOrder.getLineItemshascascade=ALLset in the relationship annotation.Synchronizing Entity Data to the Database
The state of persistent entities is synchronized to the database when the transaction with which the entity is associated commits. If a managed entity is in a bidirectional relationship with another managed entity, the data will be persisted based on the owning side of the relationship.
To force synchronization of the managed entity to the data store, invoke the
flushmethod of the entity. If the entity is related to another entity, and the relationship annotation has thecascadeelement set toPERSISTorALL, the related entity's data will be synchronized with the data store whenflushis called.If the entity is removed, calling
flushwill remove the entity data from the data store.Creating Queries
The
EntityManager.createQueryandEntityManager.createNamedQuerymethods are used to query the datastore using Java Persistence query language queries. See Chapter 24 for more information on the query language.The
createQuerymethod is used to create dynamic queries, queries that are defined directly within an application's business logic.public List findWithName(String name) { return em.createQuery( "SELECT c FROM Customer c WHERE c.name LIKE :custName") .setParameter("custName", name) .setMaxResults(10) .getResultList(); }The
createNamedQuerymethod is used to create static queries, queries that are defined in metadata using thejavax.persistence.NamedQueryannotation. Thenameelement of@NamedQueryspecifies the name of the query that will be used with thecreateNamedQuerymethod. Thequeryelement of@NamedQueryis the query.@NamedQuery( name="findAllCustomersWithName", query="SELECT c FROM Customer c WHERE c.name LIKE :custName" )Here's an example of
createNamedQuery, which uses the@NamedQuerydefined above.@PersistenceContext public EntityManager em; ... customers = em.createNamedQuery("findAllCustomersWithName") .setParameter("custName", "Smith") .getResultList();Named Parameters in Queries
Named parameters are parameters in a query that are prefixed with a colon (
:). Named parameters in a query are bound to an argument by thejavax.persistence.Query.setParameter(String name, Object value)method. In the following example, thenameargument to thefindWithNamebusiness method is bound to the:custNamenamed parameter in the query by callingQuery.setParameter.public List findWithName(String name) { return em.createQuery( "SELECT c FROM Customer c WHERE c.name LIKE :custName") .setParameter("custName", name) .getResultList(); }Named parameters are case-sensitive, and may be used by both dynamic and static queries.
Positional Parameters in Queries
You may alternately use positional parameters in queries, instead of named parameters. Positional parameters are prefixed with a question mark (
?) followed the numeric position of the parameter in the query. TheQuery.setParameter(integer position, Object value)method is used to set the parameter values.In the following example, the
findWithNamebusiness method is rewritten to use input parameters:public List findWithName(String name) { return em.createQuery( "SELECT c FROM Customer c WHERE c.name LIKE ?1") .setParameter(1, name) .getResultList(); }Input parameters are numbered starting from 1. Input parameters are case-sensitive, and may be used by both dynamic and static queries.
Persistence Units
A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store.
Persistence units are defined by the
persistence.xmlconfiguration file. The JAR file or directory whoseMETA-INFdirectory containspersistence.xmlis called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit's root.Each persistence unit must be identified with a name that is unique to the persistence unit's scope.
Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in an WAR or EAR file.
If you package the persistent unit as a set of classes in an EJB JAR file,
persistence.xmlshould be put in the EJB JAR'sMETA-INFdirectory.If you package the persistence unit as a set of classes in a WAR file,
persistence.xmlshould be located in the WAR file'sWEB-INF/classes/META-INFdirectory.If you package the persistence unit in a JAR file that will be included in a WAR or EAR file, the JAR file should be located:
The persistence.xml File
persistence.xmldefines one or more persistence units. The following is an examplepersistence.xmlfile.<persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers. It does not rely on any vendor-specific features and can therefore be deployed to any persistence provider. </description> <jta-data-source>jdbc/MyOrderDB</jta-data-source> <jar-file>MyOrderApp.jar</jar-file> <class>com.widgets.Order</class> <class>com.widgets.Customer</class> </persistence-unit> </persistence>This file defines a persistence unit named
OrderManagement, which uses a JTA-aware data sourcejdbc/MyOrderDB. Thejar-fileandclasselements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, while the class element explicitly names managed persistence classes.The
jta-data-source(for JTA-aware data sources) andnon-jta-data-source(non-JTA-aware data sources) elements specify the global JNDI name of the data source to be used by the container.