JPA Configuration

SeedStack JPA add-on supports any JPA-compliant ORM to allow your application to interface with relational databases.

Dependencies

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

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")
}

Configuration

SeedStack is able to automatically detect the JPA classes in your project, without persistence.xml file. You just have to declare the JPA units:

jpa:
  # Configured JPA units with the name of the JPA unit as key
  units: 
    myUnit:
      # The name of the data-source declared in the JDBC add-on to use
      datasource: (String)
      
      # The JPA and/or provider properties
      properties:
        property1: value1
      
      # Explicit list of classes belonging to the unit (not needed when using auto-detection)
      classes: (List<Class<?>>)
      
      # List of mapping files paths (orm.xml) if not using annotation-based mapping 
      mappingFiles: (List<String>)
      
      # The fully qualified name of the JPA provider (will be auto-detected if not specified)
      provider: (Class<? extends PersistenceProvider>)
      
      # The transaction type (local or JTA)
      transactionType: (RESOURCE_LOCAL|JTA)
      
      # Specifies how the provider must use a second-level cache
      sharedCacheMode: (ALL|NONE|ENABLE_SELECTIVE|DISABLE_SELECTIVE|UNSPECIFIED)
      
      # The validation mode to be used by the provider
      validationMode: (AUTO|CALLBACK|NONE)
      
      # The fully qualified class name of the exception handler (optional)
      exceptionHandler: (Class<? extends JpaExceptionHandler>)

  # The name of the configured unit to use if nothing is specified in the '@JpaUnit' annotation    
  defaultUnit: (String)

To dump the jpa configuration options:

mvn -q -Dargs="jpa" seedstack:config

To allow SeedStack to associate auto-detected JPA classes to a particular JPA unit, you must configure them with a class configuration tag:

classes:
  org:
    myorg:
      myapp:
        domain:
          model:
            jpaUnit: myUnit

This will assign every class in the org.myorg.myapp.domain.model package and its sub-packages to the JPA unit myUnit.

Persistence.xml file

Instead of using JPA auto-configuration, you can choose to use a standard META-INF/persistence.xml file instead. In this case:

  • Ensure that the JPA units declared in the configuration above are matching the ones specified in the persistence.xml file.
  • The datasource must be configured in the persistence.xml file and not in the configuration above.
  • The classes, mappingFiles, provider, transactionType, sharedCacheMode and validationMode configuration options above have no effect and must be specified in persistence.xml instead.
  • You can still specify JPA provider properties in the configuration. They override properties declared in the persistence.xml file if any.

Usage

To use the Entity Manager directly, simply inject it:

public class MyRepository {
    @Inject
    private EntityManager entityManager;
    
    @Transactional
    @JpaUnit("myUnit")
    public void doSomethingWithMyJpaUnit() {
        // do something
    }
}

All JPA interactions have to be done inside a transaction. Refer to the transaction documentation for details.

Example

Connection pool

In addition to the JPA add-on and the Hibernate dependencies, we’ll add an HikariCP connection pool:

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

Configuration

Assuming we are using Hibernate and an Hikari connection pool, the configuration below:

  • Defines a unit named myUnit,
  • Using the data-source myDatasource defined with the JDBC add-on,
  • Affects every JPA entity of packages org.generated.project.domain.model.* to myUnit.
jdbc:
  datasources:
    myDatasource:
      provider: org.seedstack.jdbc.internal.datasource.HikariDataSourceProvider
      url: jdbc:hsqldb:mem:testdb1
jpa:
  units:
    myUnit:
      datasource: myDatasource
      properties:
        hibernate.dialect: org.hibernate.dialect.HSQLDialect
        hibernate.hbm2ddl.auto: update
classes:
  org:
    generated:
      project:
        domain:
          model:
            jpaUnit: myUnit
   

On this page


Edit this page