Chapter 6.  Java Persistence Entity Operations

Describe how to manage entities, including using the EntityManager API and the cascade option.

[Note]

An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager interface defines the methods that are used to interact with the persistence context. The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.

The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit. A persistence unit defines the set of all classes that are related or grouped by the application, and which must be colocated in their mapping to a SINGLE database.

EntityManager Interface

The EntityManager is the primary interface used by application developers to interact with the JPA runtime. The methods of the EntityManager can be divided into the following functional categories:

  1. Transaction Association

    public EntityTransaction getTransaction();
    								

    Every EntityManager has a one-to-one relation with an EntityTransaction instance. In fact, many vendors use a single class to implement both the EntityManager and EntityTransaction interfaces. If your application requires multiple concurrent transactions, you will use multiple EntityManagers.

    You can retrieve the EntityTransaction associated with an EntityManager through the getTransaction method. Note that most most JPA implementations can integrate with an application server's managed transactions. If you take advantage of this feature, you will control transactions by declarative demarcation or through the Java Transaction API (JTA) rather than through the EntityTransaction.

  2. Entity Lifecycle Management

    EntityManagers perform several actions that affect the lifecycle state of entity instances.

    The following diagram illustrates the lifecycle of an entity with respect to the APIs presented in this section.

    The lifecycle of an Entity

    											
    public void persist(Object entity);
    								

    Transitions NEW instances to MANAGED. On the next flush or commit, the newly persisted instances will be INSERTED into the datastore.

    For a given entity A, the persist method behaves as follows:

    • If A is a NEW entity, it becomes MANAGED.

    • If A is an existing MANAGED entity, it is IGNORED. However, the persist operation cascades as defined below.

    • If A is a REMOVED entity, it becomes MANAGED.

    • If A is a DETACHED entity, an IllegalArgumentException is thrown.

    • The persist operation recurses on all relation fields of A whose cascades include CascadeType.PERSIST.

    Persisting a new entity

    This action can only be used in the context of an ACTIVE transaction.

    Persisting Objects Example:

    // create some objects
    Magazine mag = new Magazine("1B78-YU9L", "JavaWorld");
    
    Company pub = new Company("Weston House");
    pub.setRevenue(1750000D);
    mag.setPublisher(pub);
    pub.addMagazine(mag);
    
    Article art = new Article("JPA Rules!", "Transparent Object Persistence");
    art.addAuthor(new Author("Fred", "Hoyle"));
    mag.addArticle(art);
    
    // persist
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(mag);
    em.persist(pub);
    em.persist(art);
    em.getTransaction().commit();
    
    // or we could continue using the EntityManager...
    em.close();
    								

    											
    public void remove(Object entity);
    								

    Transitions managed instances to REMOVED. The instances will be deleted from the datastore on the next flush or commit. Accessing a removed entity has UNDEFINED results.

    For a given entity A, the remove method behaves as follows:

    • If A is a NEW entity, it is ignored. However, the remove operation cascades as defined below.

    • If A is an existing MANAGED entity, it becomes REMOVED.

    • If A is a REMOVED entity, it is IGNORED.

    • If A is a DETACHED entity, an IllegalArgumentException is thrown.

    • The remove operation recurses on all relation fields of A whose cascades include CascadeType.REMOVE.

    Removing the entity

    This action can only be used in the context of an ACTIVE transaction.

    Removing Objects Example:

    // assume we have an object id for the company whose subscriptions
    // we want to delete
    Object oid = ...;
    
    // deletes should always be made within transactions
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Company pub = (Company) em.find(Company.class, oid);
    for (Subscription sub : pub.getSubscriptions())
    	em.remove(sub);
    pub.getSubscriptions().clear();
    em.getTransaction().commit();
    
    // or we could continue using the EntityManager...
    em.close();
    								

    											
    public void refresh(Object entity);
    								

    Use the refresh action to make sure the persistent state of an instance is synchronized with the values in the datastore. refresh is intended for long-running optimistic transactions in which there is a danger of seeing stale data.

    For a given entity A, the refresh method behaves as follows:

    • If A is a NEW entity, it is IGNORED. However, the refresh operation cascades as defined below.

    • If A is an existing MANAGED entity, its state is REFRESHED from the datastore.

    • If A is a REMOVED entity, it is IGNORED.

    • If A is a DETACHED entity, an IllegalArgumentException is thrown.

    • The refresh operation recurses on all relation fields of A whose cascades include CascadeType.REFRESH.

    											
    public Object merge(Object entity);
    								

    A common use case for an application running in a servlet or application server is to "detach" objects from all server resources, modify them, and then "attach" them again. For example, a servlet might store persistent data in a user session between a modification based on a series of web forms. Between each form request, the web container might decide to serialize the session, requiring that the stored persistent state be disassociated from any other resources. Similarly, a client/server application might transfer persistent objects to a client via serialization, allow the client to modify their state, and then have the client return the modified data in order to be saved. This is sometimes referred to as the data transfer object or value object pattern, and it allows fine-grained manipulation of data objects without incurring the overhead of multiple remote method invocations.

    JPA provides support for this pattern by automatically detaching entities when they are serialized or when a persistence context ends. The JPA merge API re-attaches detached entities. This allows you to detach a persistent instance, modify the detached instance offline, and merge the instance back into an EntityManager (either the same one that detached the instance, or a new one). The changes will then be applied to the existing instance from the datastore.

    A detached entity maintains its persistent identity, but cannot load additional state from the datastore. Accessing any persistent field or property that was not loaded at the time of detachment has UNDEFINED results. Also, be sure NOT to alter the version or identity fields of detached instances if you plan on merging them later.

    The merge method returns a managed copy of the given detached entity. Changes made to the persistent state of the detached entity are applied to this managed instance. Because merging involves changing persistent state, you can only merge WITHIN a transaction.

    If you attempt to merge an instance whose representation has changed in the datastore since detachment, the merge operation will throw an exception, or the transaction in which you perform the merge will fail on commit, just as if a normal optimistic conflict were detected.

    For a given entity A, the merge method behaves as follows:

    • If A is a DETACHED entity, its state is COPIED into existing managed instance A' of the same entity identity, or a new managed copy of A is created.

    • If A is a NEW entity, a new MANAGED entity A' is created and the state of A is COPIED into A'.

    • If A is an existing MANAGED entity, it is IGNORED. However, the merge operation still cascades as defined below.

    • If A is a REMOVED entity, an IllegalArgumentException is thrown.

    • The merge operation recurses on all relation fields of A whose cascades include CascadeType.MERGE.

    Merging the entity

    This example demonstrates a common client/server scenario. The client requests objects and makes changes to them, while the server handles the object lookups and transactions:

    // CLIENT:
    // requests an object with a given oid
    Record detached = (Record) getFromServer(oid);
    								
    // SERVER:
    // send object to client; object detaches on EM close
    Object oid = processClientRequest();
    EntityManager em = emf.createEntityManager();
    Record record = em.find(Record.class, oid);
    em.close();
    sendToClient(record);
    								
    // CLIENT:
    // makes some modifications and sends back to server
    detached.setSomeField("bar");
    sendToServer(detached);
    								
    // SERVER:
    // merges the instance and commit the changes
    Record modified = (Record) processClientRequest();
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Record merged = (Record) em.merge(modified);
    merged.setLastModified(System.currentTimeMillis());
    merged.setModifier(getClientIdentityCode());
    em.getTransaction().commit();
    em.close();
    								

    											
    public void lock (Object entity, LockModeType mode);
    								

    This method locks the given entity using the named mode. The javax.persistence.LockModeType enum defines two modes:

    • READ: Other transactions may concurrently read the object, but cannot concurrently update it.

    • WRITE: Other transactions cannot concurrently read or write the object. When a transaction is committed that holds WRITE locks on any entites, those entites will have their version incremented even if the entities themselves did not change in the transaction.

  3. Entity Identity Management

    Each EntityManager is responsible for managing the persistent identities of the managed objects in the persistence context. The following methods allow you to interact with the management of persistent identities. The behavior of these methods is deeply affected by the persistence context type of the EntityManager.

    
    public <T> T find(Class<T> cls, Object oid);
    
    								

    This method returns the persistent instance of the given type with the given persistent identity. If the instance is already present in the current persistence context, the cached version will be returned. Otherwise, a new instance will be constructed and loaded with state from the datastore. If no entity with the given type and identity exists in the datastore, this method returns null.

    Example:

    MagazineId mi = new MagazineId();
    mi.isbn = "1B78-YU9L";
    mi.title = "JavaWorld";
    
    // updates should always be made within transactions; note that
    // there is no code explicitly linking the magazine or company
    // with the transaction; JPA automatically tracks all changes
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Magazine mag = em.find(Magazine.class, mi);
    mag.setPrice(5.99);
    Company pub = mag.getPublisher();
    pub.setRevenue(1750000D);
    em.getTransaction().commit();
    
    // or we could continue using the EntityManager...
    em.close();
    								

    
    public <T> T getReference(Class<T> cls, Object oid);
    
    								

    This method is similar to find, but does NOT necessarily go to the database when the entity is not found in cache. The implementation may construct a hollow entity and return it to you instead. Hollow entities do not have any state loaded. The state only gets loaded when you attempt to access a persistent field. At that time, the implementation may throw an EntityNotFoundException if it discovers that the entity does not exist in the datastore. The implementation may also throw an EntityNotFoundException from the getReference method itself. Unlike find, getReference does NOT return null.

    public boolean contains(Object entity);
    								

    Returns true if the given entity is part of the current persistence context, and false otherwise. Removed entities are not considered part of the current persistence context.

  4. Cache Management

    public void flush();
    								

    The flush method writes any changes that have been made in the current transaction to the datastore. If the EntityManager does not already have a connection to the datastore, it obtains one for the flush and retains it for the duration of the transaction. Any exceptions during flush cause the transaction to be marked for rollback.

    Synchronizing to the database

    Flushing requires an ACTIVE transaction. If there isn't a transaction in progress, the flush method throws a TransactionRequiredException.

    public FlushModeType getFlushMode();
    public void setFlushMode(FlushModeType flushMode);
    								

    The EntityManager's FlushMode property controls whether to flush transactional changes before executing queries. This allows the query results to take into account changes you have made during the current transaction. Available javax.persistence.FlushModeType constants are:

    • COMMIT: Only flush when committing, or when told to do so through the flush method. Query results may not take into account changes made in the current transaction.

    • AUTO: The implementation is permitted to flush before queries to ensure that the results reflect the most recent object state.

    You can also set the flush mode on individual Query instances.

    public void clear();
    								

    Clearing the EntityManager effectively ends the persistence context. All entities managed by the EntityManager become DETACHED.

    Detaching the entity

  5. Query Factory

    public Query createQuery(String query);
    								

    Query objects are used to find entities matching certain criteria. The createQuery method creates a query using the given Java Persistence Query Language (JPQL) string.

    public Query createNamedQuery(String name);
    								

    This method retrieves a query defined in metadata by name. The returned Query instance is initialized with the information declared in metadata.

    public Query createNativeQuery(String sql);
    public Query createNativeQuery(String sql, Class resultCls);
    public Query createNativeQuery(String sql, String resultMapping);
    								

    Native queries are queries in the datastore's native language. For relational databases, this the Structured Query Language (SQL).

  6. Closing

    public boolean isOpen();
    public void close();
    								

    When an EntityManager is no longer needed, you should call its close method. Closing an EntityManager releases any resources it is using. The persistence context ends, and the entities managed by the EntityManager become DETACHED. Any Query instances the EntityManager created become INVALID. Calling any method other than isOpen on a closed EntityManager results in an IllegalStateException. You cannot close a EntityManager that is in the middle of a transaction.

    If you are in a managed environment using injected entity managers, you SHOULD NOT close them.

NOTE:

The persist, merge, remove, and refresh methods MUST be invoked within a transaction context when an entity manager with a transaction-scoped persistence context is used. If there is no transaction context, the javax.persistence.TransactionRequiredException is thrown.

The find and getReference methods are NOT required to be invoked within a transaction context. If an entity manager with transaction-scoped persistence context is in use, the resulting entities will be DETACHED; if an entity manager with an extended persistence context is used, they will be MANAGED.

The Query and EntityTransaction objects obtained from an entity manager are valid while that entity manager is OPEN.

If the argument to the createQuery method is not a valid Java Persistence query string, the IllegalArgumentException may be thrown or the query execution will fail. If a native query is not a valid query for the database in use or if the result set specification is incompatible with the result of the query, the query execution will fail and a PersistenceException will be thrown when the query is executed. The PersistenceException should wrap the underlying database exception when possible.

Runtime exceptions thrown by the methods of the EntityManager interface will cause the current transaction to be ROLLED BACK.

The methods close, isOpen, joinTransaction, and getTransaction are used to manage application-managed entity managers and their lifecycle.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine


webhosting provided by ixvar inc