ObjectDB ObjectDB

Updating JPA Entity Objects

Modifying existing entity objects that are stored in the database is based on transparent persistence, which means that changes are detected and handled automatically.

This page covers the following topics:

Transparent Update

Once an entity object is retrieved from the database (no matter which way) it can simply be modified in memory from inside an active transaction:

  Employee employee = em.findfind(entityClass, primaryKey)EntityManager's methodFind by primary key.See JavaDoc Reference Page...(Employee.class, 1);

  em.getTransactiongetTransaction()EntityManager's methodReturn the resource-level EntityTransaction object.See JavaDoc Reference Page...().beginbegin()EntityTransaction's methodStart a resource transaction.See JavaDoc Reference Page...();
  employee.setNickname("Joe the Plumber");
  em.getTransactiongetTransaction()EntityManager's methodReturn the resource-level EntityTransaction object.See JavaDoc Reference Page...().commitcommit()EntityTransaction's methodCommit the current resource transaction, writing any 
 unflushed changes to the database.See JavaDoc Reference Page...();

The entity object is physically updated in the database when the transaction is committed. If the transaction is rolled back and not committed the update is discarded.

On commit the persist operation can be cascaded from all the entity objects that have to be stored in the database, including from all the modified entity objects. Therefore, entity objects that are referenced from modified entity objects by fields that are marked with CascadeTypejavax.persistence.CascadeTypeJPA enumDefines the set of cascadable operations that are propagated to the associated entity.See JavaDoc Reference Page....PERSISTCascadeType.PERSISTenum constantCascade persist operationSee JavaDoc Reference Page... or CascadeTypejavax.persistence.CascadeTypeJPA enumDefines the set of cascadable operations that are propagated to the associated entity.See JavaDoc Reference Page....ALLCascadeType.ALLenum constantCascade all operationsSee JavaDoc Reference Page... are also persisted. If global cascade persist is enabled all the reachable entity objects that are not managed yet are also persisted.

Automatic Change Tracking

As shown above, an update is achieved by modifying a managed entity object from within an active transaction. No EntityManagerjavax.persistence.EntityManagerJPA interfaceInterface used to interact with the persistence context.See JavaDoc Reference Page...'s method is invoked to report the update. Therefore, to be able to apply database updates on commit, ObjectDB must detect changes to managed entities automatically. One way to detect changes is to keep a snapshot of every managed object when it is retrieved from the database and to compare that snapshot to the actual managed object on commit. A more efficient way to detect changes automatically is described in the Enhancer section in chapter 5.

However, detecting changes to arrays requires using snapshots even if the entity classes are enhanced. Therefore, for efficiency purposes, the default behavior of ObjectDB ignores array changes when using enhanced entity classes:

    Employee employee = em.findfind(entityClass, primaryKey)EntityManager's methodFind by primary key.See JavaDoc Reference Page...(Employee.class, 1);

    em.getTransactiongetTransaction()EntityManager's methodReturn the resource-level EntityTransaction object.See JavaDoc Reference Page...().beginbegin()EntityTransaction's methodStart a resource transaction.See JavaDoc Reference Page...();
    employee.projects[0] = new Project(); // not detected automatically
    JDOHelperjavax.jdo.JDOHelperJDO classThis class can be used by a JDO-aware application to call the JDO behavior
 of PersistenceCapable instances without declaring them to be
 PersistenceCapable.See JavaDoc Reference Page....makeDirtymakeDirty(pc, fieldName)JDOHelper's static methodExplicitly mark the parameter instance and field dirty.See JavaDoc Reference Page...(employee, "projects"); // reported as dirty
    em.getTransactiongetTransaction()EntityManager's methodReturn the resource-level EntityTransaction object.See JavaDoc Reference Page...().commitcommit()EntityTransaction's methodCommit the current resource transaction, writing any 
 unflushed changes to the database.See JavaDoc Reference Page...();

As demonstrated above, array changes are not detected automatically (by default) but it is possible to report a change explicitly by invoking the JDO's makeDirtymakeDirty(pc, fieldName)JDOHelper's static methodExplicitly mark the parameter instance and field dirty.See JavaDoc Reference Page... method.

Alternatively, ObjectDB can be configured to detect array changes using snapshots as well as when enhanced entity classes are in use.

It is usually recommended to use collections rather than arrays when using JPA. Collections are more portable to ORM JPA implementations and provide better automatic change tracking support.

UPDATE Queries

UPDATE queries provide an alternative way of updating entity objects in the database. Modifying objects using an UPDATE query may be useful especially when many entity objects have to be modified in one operation. The UPDATE Queries in JPA/JPQL in chapter 4 explains how to use JPA UPDATE queries.