JPA Configuration, Step-by-Step

This guide details step-by-step how to configure JPA in SeedStack, to access an existing relational database.

Add the dependencies

JPA add-on

First, add the SeedStack JPA add-on:

<dependency>
    <groupId>org.seedstack.addons.jpa</groupId>
    <artifactId>jpa</artifactId>
</dependency>
Show version
dependencies {
    compile("org.seedstack.addons.jpa:jpa:4.0.3")
}

JPA provider

Beyond the JPA add-on, an actual JPA implementation, called a JPA provider, is required:

Hibernate is a very popular JPA implementation. When using Hibernate, SeedStack is able to stream results from the database without putting them all in memory (useful when retrieving for result sets).

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("org.hibernate:hibernate-entitymanager:LATEST_VERSION_HERE")
}

Connection pooling

If you want connection pooling, you have to add the dependency for one of the following pooling libraries:

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("com.zaxxer:HikariCP:LATEST_VERSION_HERE")
}

<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("com.mchange:c3p0:LATEST_VERSION_HERE")
}

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("org.apache.commons:commons-dbcp2:LATEST_VERSION_HERE")
}

JDBC driver

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("com.h2database:h2:LATEST_VERSION_HERE")
}

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("org.hsqldb:hsqldb:LATEST_VERSION_HERE")
}

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("org.postgresql:postgresql:LATEST_VERSION_HERE")
}

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("org.mariadb.jdbc:mariadb-java-client:LATEST_VERSION_HERE")
}

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>
dependencies {
    compile("com.oracle.jdbc:ojdbc8:LATEST_VERSION_HERE")
}

The JDBC driver can be obtained from the directory of your DB2 installation.

Configure the datasource

JPA relies on a JDBC datasource to access the database. We are now going to configure such datasource through the SeedStack JDBC add-on (pulled transitively by the JPA add-on):

jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:h2:mem:mydb
jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:hsqldb:mem:mydb
jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:postgresql://localhost:5740/mydb
jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:mariadb://localhost:3306/mydb
jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:oracle:thin:@localhost:1521:mydb
jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:db2://localhost:5021/mydb

Configure the JPA unit

JPA organizes persisted classes in buckets called «JPA units». All classes persisted within a JPA unit share a common datasource and configuration. A local transaction is bound to a JPA unit (it cannot span over multiple units). We are now going to configure a JPA unit that uses the datasource we specified before:

jpa:
  units:
    myUnit:
      datasource: myDatasource
      properties:
        hibernate.dialect: org.hibernate.dialect.H2Dialect
jpa:
  units:
    myUnit:
      datasource: myDatasource
      properties:
        hibernate.dialect: org.hibernate.dialect.HSQLDialect
jpa:
  units:
    myUnit:
      datasource: myDatasource
      properties:
        hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
jpa:
  units:
    myUnit:
      datasource: myDatasource
      properties:
        hibernate.dialect: org.hibernate.dialect.MariaDB53Dialect
jpa:
  units:
    myUnit:
      datasource: myDatasource
      properties:
        hibernate.dialect: org.hibernate.dialect.Oracle12cDialect
jpa:
  units:
    myUnit:
      datasource: datasource1
      properties:
        hibernate.dialect: org.hibernate.dialect.DB2Dialect

Affect persisted classes to unit

To automatically configure the JPA unit, SeedStack automatically detected persisted class (i.e. JPA entities). However it needs additional information to associate a particular class to a particular unit. This is done with class configuration:

classes:
  org:
    generated:
      project:
        domain:
          model:
            jpaUnit: myUnit

Every JPA entity inside the package org.generated.project.domain.model and its subpackages will be associated with the JPA unit myUnit.

This «tag» can be specified at any package level and an upper-level tag can be overridden by a lower-level tag.

Write your first JPA entity


On this page


Edit