441 words

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.

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:

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 operation
See 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 operations
See 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:

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 for 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.


This documentation explains how to use JPA in the context of the ObjectDB Object Database but mostly relevant
also for ORM JPA implementations, such as Hibernate (and HQL), EclipseLink, TopLink, OpenJPA and DataNucleus.
ObjectDB is not an ORM JPA implementation but an Object Database (ODBMS) for Java with built in JPA 2 support.