Creating a JPA configuration

JPA is intended to provide persistence services saving transient Java instances to a database. For this purpose we have to provide:

  • The type of database (Oracle, DB2, Mysql,...)

  • JDBC driver class name.

  • JDBC connection parameters

    • Server name

    • port

    • user name

    • password

  • A list of classes to be mapped

  • Parameters defining the log level, whether generated SQL code shall be logged etc.

JPA offers an XML based configuration syntax typically residing in src/main/resources/META-INF/persistence.xml. We show a toy example of a hibernate.cfg.xml configuration file mapping just one class hibintro.v1.model.User to a Mysql database server:

Figure 929. A basic persistence.xml JPA configuration file.
<persistence
    version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    
    <!-- 
         The <code>persistence.xml</code> file configures at least one persistence unit;
         each unit must have a unique name.
    -->
    <persistence-unit name="entitytemplatePU">
        
        <!-- 
            Hibernate will scan your classpath for mapped classes and add them automatically
            to your persistence unit.
             <exclude-unlisted-classes>false</exclude-unlisted-classes>
        -->
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        
        <!-- 
            Disabling the previous option "exclude-unlisted-classes" requires entity classes to
            be listed explicitely. You may want to uncomment the following definition.

            <class>my.base.url.Airline</class>
        -->
        
        <!-- 
            Standard or vendor-specific options can be set as properties on a persistence unit.
            Any standard properties have the <code>javax.persistence</code> name prefix, Hibernate's
            settings use <code>hibernate</code>
        -->
        <properties>
            <!-- 
                JDBC database connection parameter
            -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hdm?useSSL=false"/>
            <property name="javax.persistence.jdbc.user" value="hdmuser"/>
            <property name="javax.persistence.jdbc.password" value="XYZ"/>
            
            <!-- 
                The JPA engine should drop and re-create the SQL schema in the database
                automatically when it boots. This is ideal for automated testing, when
                you want to work with a clean database for every test run.
            -->
            <property
                name="javax.persistence.schema-generation.database.action"
                value="drop-and-create"/>
            
            <!-- 
                When printing SQL in logs, let Hibernate format the SQL nicely and generate
                comments into the SQL string so we know why Hibernate executed the SQL statement.
            -->
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true"/>
            
            <!-- Enable Hibernate scanning for entity classes and adding them automatically
                but not for hbm.xml files. -->
            <property name="hibernate.archive.autodetection" value="class"/>

        </properties>
    </persistence-unit>
    
</persistence>

This file may be edited with a simple text editor. Note the persistence_2_1.xsd schema which allows for validation.

The Eclipse Jboss Tools Eclipse plugin provides a configuration editor simplifying this task. They may be installed on top of Eclipse in several ways. The following video shows some of its features.