Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
To start a session with the datastore, you use the
EntityManagerFactory to create an EntityManager.
You must create a new EntityManager for each request handler,
and close it when you’re done:
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import myapp.EMF; // where "myapp" is your app's package
// ...
EntityManagerFactory emf = EMF.get();
EntityManager em = null;
try {
em = emf.createEntityManager();
// ... do datastore stuff ...
} finally {
if (em != null)
em.close();
}
To create a new data object, you construct the data class and then
call the EntityManager’s persist() method with
the object: