8.3. Lifecycle Examples
Prev | Chapter 8. EntityManager | Next

## 8.3. Lifecycle Examples

The examples below demonstrate how to use the lifecycle methods presented in the previous section. The examples are appropriate for out-of-container use. Within a container, `EntityManager`s are usually injected, and transactions are usually managed. You would therefore omit the `createEntityManager` and `close` calls, as well as all transaction demarcation code.

Example 8.1. Persisting Objects

    // 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 ("EJB Rules!", "Transparent Object Persistence");
    art.addAuthor (new Author ("Fred", "Hoyle"));
    mag.addArticle (art);

    // Persist them.
    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 ();

Example 8.2. Updating Objects

    Magazine.MagazineId mi = new Magazine.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; EJB 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 ();

Example 8.3. Removing Objects

    // 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 ();

Example 8.4. Detaching and Merging

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:
    // Sends 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 commits 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 ();

Prev | Up | Next
8.2. Entity Lifecycle Management | Home | 8.4. Entity Identity Management