Versions Compared

Key

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

...

Code Block
XML
XML
borderStylesolid
titlepersistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 ...
 ...
</persistence>

<persistence-unit>

The <persistence> element can consist of more than one <persistence-unit> element each describing a persistence unit. The persistence-unit element consists of the following attributes.

Attribute : name

This attribute is required. The name attribute defines the name for the persistence unit. It uniquely identifies a persistence context. This name is used to identify a persistence unit referred to by the PersistenceContext and PersistenceUnit annotations. It is also referred while creating an entity manager factory object. The following note illustrates the usage in an application.

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 EntityManager object can be created.

Code Block
JAVA
JAVA
borderStylesolid

@PersistenceUnit(unitName="Account")


private EntityManagerFactory emf;

Attribute : transaction-type

This attribute can have the following values.

...

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 by the application.