Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note

1. The below annotation injects EntityManager object that corresponds to Tutorial persistence unit
to the variable em .

Code Block
JAVA
JAVA
borderStylesolid
  @PersistenceContext(unitName="Tutorial")
  private EntityManager em; 

2. The below annotation injects EntityManagerFactory object that corresponds to Account
persistence unit to the variable emf. From the emf, the EntityManager object can be created.

Code Block
JAVA
JAVA
borderStylesolid
@PersistenceUnit(unitName="Account")
private EntityManagerFactory emf;

...

Note

1. The below declaration is for a persistence unit named Account and the transaction type is JTA.

Code Block
XML
XML
borderStylesolid
<persistence-unit name="Account" transaction-type="JTA">
...
...
</persistence-unit>

2. The below declaration is for a persistence unit named Account and the transaction type is
RESOURCE_LOCAL.

Code Block
XML
XML
borderStylesolid
 
<persistence-unit name="Account" transaction-type="RESOURCE_LOCAL">
...
...
</persistence-unit>

Please note that there are two types of entity managers and corresponding persistence contexts. These are container managed entity manager and application managed entity manager.

The container managed entity manager is the one whose persistence context is managed by container. The persistence context is propagated along the active transaction. The persistence scope of the container managed entity manager is transaction by default. The transaction type of the entity manager is always JTA. The EntityManager object is injected by the container to the application.

The application managed entity manager is the one whose persistence context is managed by the application. The persistence context is not propagated along the transaction. The persistence context will be active even after the current transaction completes. The transaction type of the entity manager is RESOURCE_LOCAL by default. The EntityManager object should be created by using EntityManagerFactory object in the application.

...