An entity manager is associated with a group of managed entities called a "persistence context". Entities within a persistence context are associated with an entity manager instance. To use an entity manager, you first need to obtain an entity manager from the container.
To obtain an entity manager:
The IDE adds the code to obtain an entity manager instance.
How the entity manager instance is obtained depends upon your project. For example, if you are using a container-managed entity manager, you can obtain an entity manager instance by injecting the entity manager resource directly using the @PersistenceContext annotation. If entities in your project are application-managed, you first need to obtain an EntityManagerFactory before you can obtain an entity manager. You can obtain an EntityManagerFactory by using the @PersistenceUnit annotation in a Java EE container, or by calling Persistence.createEntityManagerFactory in a Java SE environment.
For more on entity managers, see the Java EE 5 Tutorial: