Persistence Unit

Overview

A persistence unit is a fundamental concept in Java Persistence API (JPA) applications. It represents a configuration that pairs domain model class mappings with a database connection, along with various configuration settings. Every JPA application requires at least one persistence unit to function. The persistence unit is essential as it defines the set of all entity classes managed by the EntityManager and includes the necessary database connection details and other settings required for persistence.

Configuring a Persistence Unit

The configuration of a persistence unit is typically done in a standard configuration file named persistence.xml, which is located on the classpath in the META-INF directory. This file includes several important settings such as:

  • Persistence Unit Name: A unique identifier for the persistence unit.
  • Provider: Specifies the JPA provider, such as Hibernate.
  • JDBC Properties: Includes the database driver, URL, username, and password.
  • Hibernate Dialect: Specifies the SQL dialect to be used by Hibernate.
  • SQL Formatting Options: Options to format and display SQL statements.

Example Configuration

Below is an example of a persistence.xml configuration file for a "Hello World" application:

Listing 2.2 The persistence.xml configuration file

Path: Ch02/helloworld/src/main/resources/META-INF/persistence.xml

<persistence 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_2_0.xsd"
             version="2.0">

    <persistence-unit name="ch02">                                          #1
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> #1
        <properties>
            <property name="javax.persistence.jdbc.driver"                  #2
                      value="com.mysql.cj.jdbc.Driver"/>                    #3
            <property name="javax.persistence.jdbc.url"                     #4
             value="jdbc:mysql://localhost:3306/CH02?serverTimezone=UTC "/> #4
            <property name="javax.persistence.jdbc.user" value="root"/>     #5
            <property name="javax.persistence.jdbc.password" value=""/>     #6

            <property name="hibernate.dialect"                              #7
                   value="org.hibernate.dialect.MySQL8Dialect"/>            #7

            <property name="hibernate.show_sql" value="true"/>              #8
            <property name="hibernate.format_sql" value="true"/>            #9

            <property name="hibernate.hbm2ddl.auto" value="create"/>        #10
        </properties>
    </persistence-unit>

</persistence>
  • #1: The persistence.xml file configures at least one persistence unit; each unit must have a unique name.
  • #2: As JPA is only a specification, the vendor-specific PersistenceProvider implementation of the API is indicated. Here, the persistence is backed by a Hibernate provider.
  • #3: Indicates the JDBC properties, specifically the driver.
  • #4: The URL of the database.
  • #5: The username for database access.
  • #6: The password for database access. In this example, there is no password set.
  • #7: The Hibernate dialect is set to MySQL8, as the database to interact with is MySQL Release 8.0.
  • #8: Configures Hibernate to show the SQL code while executing.
  • #9: Configures Hibernate to format the SQL nicely and generate comments in the SQL string.
  • #10: Configures Hibernate to create the database from scratch every time the program is executed, which is ideal for automated testing with a clean database for each test run.

This configuration file is crucial for setting up the persistence context in a JPA application, ensuring that the application can interact with the database effectively.