Update
Managed vs. Detached Entities¶
An entity can be either managed or detached.
Managed Entity
An entity that is currently tracked by the EntityManager. Any object we retrieve from the database via a READ operation will be managed.Detached Entity
An entity that is currently not connected to the EntityManager. Any locally created object is said to be detached.Customer c = new Customer();
For example, when we were persisting a new entity to the database (INSERT), the object was detached until we called em.persist, then added to the database when the transaction was committed.

Performing UPDATEs¶
Modifying a managed entity will result in a change to the database (UPDATE) upon a transaction commit (but not until the commit). Whereas, changing a detached entity has no effect on the database.
* You do not need to call any additional EntityManager methods.
This is confusing... let's look at an example:
public class Example {
public void illustrateManagedVsDetached() {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("VideoStore");
EntityManager em = emf.createEntityManager();
// open a transaction
em.getTransaction().begin();
// create a "detached" customer entity
Customer detachedCustomer = new Customer();
// retrieve a "managed" customer entity
Customer managedCustomer = em.find(Customer.class, 1);
// update the values of the detached entity
detachedCustomer.setFirstName("Nope");
detachedCustomer.setLastName("Not Gonna Happen");
// update the values of the managed entity
managedCustomer.setFirstName("Will Update");
managedCustomer.setLastName("On Commit");
// actually make changes
em.getTransaction().commit();
// -> the detached entity's changes were not saved
// -> the managed entity's changes HAVE been saved
}
}

- When you retrieve your EntityManager from an EntityManagerFactory, your
findorcreateQuerymethods return managed entities.
Determining if Entities are Managed¶
The following options can be used for complex logic in a DAO, though most of the time they will not be necessary.
-
Call EntityManager's
containsto determine if a particular entity is managed or detached. -
containsreturns true if the entity is managed. -
To detach an entity, pass it to EntityManager’s
detachmethod. -
Call EntityManager's
clearmethod to detach all managed entities. -
Pass a detached entity to EntityManager’s
mergemethod to get back a managed version. -
The passed-in entity remains detached, but the returned object is managed.
-
Call EntityManager's
refreshto reload data into an entity from the database, replacing any pending changes.
Drill¶
In your ActorDAO create the following abstract method:
public Actor update(int id, Actor actor);Implement the
updatemethod in your ActorDAOImpl:Use the provided
idto retrieve a managed Actor entity (em.find).Once you retrieve the correct Actor, use its set methods to update the managed Actor entity with the values in the Actor parameter passed into the method.
public Actor update(int id, Actor updatedActor){ // ... Actor managed = em.find(Actor.class, id); managed.setFirstName(updatedActor.getFirstName()); // ... return managed; }
On
commit()the managed entity will be updated.Return the updated managed entity.
To test that your
updateis working, modify ActorDAOTest by commenting out yourcreate(we don't want to create lots of duplicates), and instead usingupdateto change the first and last name of the Actor you created in the previous Drill.