×
☰ See All Chapters

JPA Persistence Unit

JPA persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store. Persistence units are defined by the persistence.xml configuration file. The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root. Each persistence unit must be identified with a name that is unique to the persistence unit’s scope. Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in a WAR or EAR file. If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR’s META-INF directory. If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.

If you package the persistence unit in a JAR file that will be included in a WAR or EAR file, the JAR file should be located as below:

  1. In the WEB-INF/lib directory of a WAR. 

  2. In the top-level of an EAR file. 

  3. In the EAR file’s library directory. 

The root element of a persistence.xml file is persistence, which then contains one or more persistence-unit definitions. The root element should include the version attribute with the appropriate version, 1.0 for a version 1.0 file and 2.0 for a version 2.0 file. Each persistence unit describes the configuration for the entity managers created by the persistence unit's entity manager factory.

Elements and attributes of persistence unit

The persistence unit can specify below list of elements and attributes:

Elements and Attributes

Description

name

This is the name we pass to the Persistence.createEntityManagerFactory methods to get EntityManagerFactory. The name attribute is required.

transaction-type

Whether to use managed (JTA) or local (RESOURCE_LOCAL) transaction management.

provider

 

If you are using a third-party JPA implementation, this element names its implementation of the PersistenceProvider bootstrapping interface.

Set the provider to org.hibernate.ejb.HibernatePersistence to use Hibernate implementation of JPA.

jta-data-source

 

The JNDI name of a JDBC DataSource that is automatically enlisted in JTA transactions. This may be an XA DataSource.

non-jta-data-source

The JNDI name of a JDBC DataSource that is not enlisted in JTA transactions.

mapping-file

 

The resource names of XML mapping files for entities and embeddable classes. You can also specify mapping information in an orm.xml file in your META-INF directory. If present, the orm.xml mapping file will be read automatically. This we have to use if we are not using annotations in our application.

jar-file

The names of jar files containing entities and embeddable classes. The implementation will scan the jar for annotated classes.

clas

The class names of entities and embeddable classes.

properties

This element contains nested property elements used to specify vendor-specific settings. Each property has a name attribute and a value attribute.

Example

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="https://java.sun.com/xml/ns/persistence"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://java.sun.com/xml/ns/persistence

             https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

        version="2.0">

 

        <persistence-unit name="StudentPU">

                <provider>org.hibernate.ejb.HibernatePersistence</provider>

                <properties>

                        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/study" />

                        <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver" />

                        <property name="hibernate.connection.username" value="root" />

                        <property name="hibernate.connection.password" value="root" />

                        <property name="hibernate.archive.autodetection" value="class" />

                        <property name="hibernate.show_sql" value="true" />

                        <property name="hibernate.format_sql" value="true" />

                        <property name="hbm2ddl.auto" value="update" />

                </properties>

        </persistence-unit>

</persistence>

 


All Chapters
Author