Updating JPA Entities
Modifying existing entities in the database relies on transparent persistence, which automatically detects and handles changes.
Transparent update
After an entity is retrieved from the database, you can modify it in memory within an active transaction:
Employee employee = em.findjakarta.persistence.EntityManager.find(Class,Object)Find by primary key.(Employee.class, 1); em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().beginjakarta.persistence.EntityTransaction.begin()Start a resource transaction.(); employee.setNickname("Joe the Plumber"); em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().commitjakarta.persistence.EntityTransaction.commit()Commit the current resource transaction, writing any unflushed changes to the database.();
The entity is updated in the database when the transaction is committed. If the transaction is rolled back, the update is discarded.
On commit, the persist operation can be cascaded from all entities that need to be stored, including all modified entities. Therefore, entities that are referenced by modified entities through fields that are marked with CascadeTypejakarta.persistence.CascadeTypeDefines the set of cascadable operations that are propagated to the associated entity..PERSISTjakarta.persistence.CascadeType.PERSISTCascade the persist operation or CascadeTypejakarta.persistence.CascadeTypeDefines the set of cascadable operations that are propagated to the associated entity..ALLjakarta.persistence.CascadeType.ALLCascade all operations are also persisted. If global cascade persist is enabled, all reachable entities that are not yet managed are also persisted.
Automatic change tracking
As shown previously, you update an entity by modifying it within an active transaction. You do not need to invoke an EntityManagerjakarta.persistence.EntityManagerInterface used to interact with the persistence context. method to report the update. Therefore, to apply database updates on commit, ObjectDB must automatically detect changes to managed entities. One detection method is to save a snapshot of each managed object when it is retrieved from the database and then compare that snapshot to the object's current state on commit. A more efficient method 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, ObjectDB by default ignores array changes when entity classes are enhanced:
Employee employee = em.findjakarta.persistence.EntityManager.find(Class,Object)Find by primary key.(Employee.class, 1); em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().beginjakarta.persistence.EntityTransaction.begin()Start a resource transaction.(); employee.projects[0] = new Project(); // not detected automatically JDOHelper.makeDirty(employee, "projects"); // reported as dirty em.getTransactionjakarta.persistence.EntityManager.getTransaction()Return the resource-level EntityTransaction object.().commitjakarta.persistence.EntityTransaction.commit()Commit the current resource transaction, writing any unflushed changes to the database.();
As demonstrated previously, array changes are not detected automatically by default, but you can report a change explicitly by invoking the JDO makeDirty method.
Alternatively, you can configure ObjectDB to detect array changes by using snapshots, even when enhanced entity classes are in use.
We recommend using collections rather than arrays when you use JPA. Collections are more portable across ORM JPA implementations and provide better support for automatic change tracking.
UPDATE queries
UPDATE queries provide an alternative way to update entities in the database. Using an UPDATE query is useful, especially when you must modify many entities in a single operation. The UPDATE Queries in JPA/JPQL section in Chapter 4 explains how to use JPA UPDATE queries.