Entity Manager

As you read in the guide for EntityManagerFactory , to control the persistence of your objects you will require at least one EntityManagerFactory . Once you have obtained this object you then use this to obtain an EntityManager . An EntityManager provides access to the operations for persistence of your objects. This short guide will demonstrate some of the more common operations.

You obtain an EntityManager as follows

EntityManager em = emf.createEntityManager();

In general you will be performing all operations on a EntityManager within a transaction, whether your transactions are controlled by your J2EE container, by a framework such as Spring, or by locally defined transactions. In the examples below we will omit the transaction demarcation for clarity.

Persisting an Object

The main thing that you will want to do with the data layer of a JPA-enabled application is persist your objects into the datastore. As we mentioned earlier, a EntityManagerFactory represents the datastore where the objects will be persisted. So you create a normal Java object in your application, and you then persist this as follows


This will result in the object being persisted into the datastore, though clearly it will not be persistent until you commit the transaction. The LifecycleState of the object changes from Transient to PersistentClean (after persist()), to Hollow (at commit).

Using an Objects identity

Once you have persisted an object, it has an "identity". This is a unique way of identifying it. When you specify the persistence for the class you specified an id class so you can create the identity from that. So what ? Well the identity can be used to retrieve the object again at some other part in your application. So you pass the identity into your application, and the user clicks on some button on a web page and that button corresponds to a particular object identity. You can then go back to your data layer and retrieve the object as follows

Object obj = em.find(cls, id);

where cls is the class of the object you want to find, and id is the identity.

Deleting an Object

When you need to delete an object that you had previous persisted, deleting it is simple. Firstly you need to get the object itself, and then delete it as follows

Object obj = em.find(cls, id);  // Retrieves the object to delete

Modifying a persisted Object

To modify a previously persisted object you take the object and update it in your code. When you are ready to persist the changes you do the following

Object updatedObj = em.merge(obj)

Refreshing a persisted Object

When you think that the datastore has more up-to-date values than the current values in a retrieved persisted object you can refresh the values in the object by doing the following


This will do the following

  • Refresh all fields that are to be eagerly fetched from the datastore
  • Unload all loaded fields that are to be lazily fetched.

If the object had any changes they will be thrown away by this step, and replaced by the latest datastore values.