JPA - Persistence Unit

> JPA - Java Persistence API (JPA)

1 - 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:

Advertising

3 - Attributes

3.1 - Name

In a Java EE environment, ensure that the persistence unit name is unique within each module.

3.2 - Transaction-type

The transaction type attribute defines which container is used for the entity manager:

  • RESOURCE_LOCAL = JSE
  • JTA = J2EE

See JPA - Entity Manager

4 - 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
Advertising

5 - Documentation / Reference

jpa/persistence_unit.txt · Last modified: 2013/12/22 14:44 by gerardnico