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 928. A basic persistence.xml JPA configuration file.
         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.
            Disabling the previous option "exclude-unlisted-classes" requires entity classes to
            be listed explicitely. You may want to uncomment the following definition.

            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>
                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.
                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"/>


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.