Obtaining an Entity Manager

See Also

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:

  1. Open the class where you want to add the entity manager resource.
  2. Right-click in the Source Editor and choose Persistence > Use Entity Manager from the pop-up menu.

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:

See Also
Working with Enterprise Applications
About Entity Managers

Legal Notices