JPA - Persistence Unit
Table of Contents
About
A persistence unit is a set of class (entities,…) that are related or grouped by your application, and which must be collocated in their mapping to a single database.
The persistence unit name is the input parameter of an EntityManager.
A persistence unit includes the following:
- An entity manager factory and its entity managers, together with their configuration information (property).
- The set of classes managed by the entity managers.
Articles Related
Attributes
Name
In a Java EE environment, ensure that the persistence unit name is unique within each module.
Transaction-type
The transaction type attribute defines which container is used for the entity manager:
- RESOURCE_LOCAL = JSE
- JTA = J2EE
Example
Example of a persistence unit configuration in the persistence.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myPersistenceUnitName" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.oracle.ticketsystem.beans.Tickethistory</class>
<class>com.oracle.ticketsystem.beans.Ticket</class>
<class>com.oracle.ticketsystem.beans.Technicianprivatedata</class>
<class>com.oracle.ticketsystem.beans.Product</class>
<class>com.oracle.ticketsystem.beans.Department</class>
<class>com.oracle.ticketsystem.beans.Technician</class>
<properties>
<property name="eclipselink.target-database" value="Derby"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/jpatutorial;create=true"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
where:
- the node <persistence-unit> defines the persistence unit where you can find the name and transaction-type attribute of the persistence unit
- the node <provider> defines the provider
- the node <class> defines the entities
- the node <property> defines the provider property
Documentation / Reference
- See chapter 8.1 of the specification