Openliberty JPA setup

created onJanuary 21, 2022

required features

  • Java Database Connectivity

and one of the followoing features:

  • Java Persistence API, includes Eclipselink as provider, or
  • Java Persistence API Container, for other providers like hibernate plus a JPA provider

configuration

configuration files / locations

. ├── pom.xml └── src └── main    │      ├── liberty    │   └── config │ └── server.xml    ├── resources    │   └── META-INF │ └── persistence.xml ...

server.xml

In the server.xml, the required features are enabled, the location of the database driver is specified and the datasource is configured.

enabling the required features

When enabling the required features, check if an already enabled feature provides the Java Database Connectivity Feature and the Java Persistence API feature. An example of this is Web Profile Feature:

<featureManager> <feature>webProfile-8.0</feature> <!-- ... some other features --> </featureManager>

If the two required features are not enabled in the featureManager element, add the two required features. for the Java Database Connectivity Feature, use the latest version as it is backwards compatible. i.e., any database driver working with jdbc-4.1 will work with jdbc-4.3.

<featureManager> <!-- ... some other features --> <feature>jdbc-4.3</feature> <feature>jpa-2.2</feature> <!-- ... some other features --> </featureManager>

specifies the Openliberty installation directory. the version part of the database driver jar file name can be replaced with an asterix ('*'). doing so, the server.xml does not have to be updated in case of a database driver update.

the following library configuration example given in Open Liberty docs Relational database connections with JDBC did not work in a simple maven project, run with mvn liberty:run:

<library id="jdbcLib"> <fileset dir="jdbc" includes="*.jar"/> </library>

providing secure credentials for datasource

Encrypt the database access password with the shell utility with AES encoding:

$(find target -name securityUtility) encode --encoding=aes Enter text: Re-enter text: {aes}AEKVY1lsgycJ3HhcmHb2hfqC4CzRW6c8jnhKuUA0ID1i $

The encrypted password is used in the configuration of the datasource.

configuration of driver lib and datasource

<library id="jdbcLib"> <fileset dir="${wlp.install.dir}/lib" includes="postgresql-*.jar"/> </library> <dataSource id="whateverdatasource" type="javax.sql.ConnectionPoolDataSource" jndiName="jdbc/whateverdb"> <jdbcDriver libraryRef="jdbcLib"/> <properties.postgresql serverName="idoru" portNumber="5432" databaseName="whateverdb" user="alice" password="{aes}AEKVY1lsgycJ3HhcmHb2hfqC4CzRW6c8jnhKuUA0ID1i"/> </dataSource>

persistence.xml

In the persistence.xml the JPA PU (Persistence Unit) is configured. The JTA datasource matches the JNDI Name of the datasource configured in the server.xml

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.2" 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_2.xsd"> <persistence-unit name="jpa-unit" transaction-type="JTA"> <jta-data-source>jdbc/whateverdb</jta-data-source> </persistence-unit> </persistence>

maven: pom.xml

In the plugins configuration section of the build section of the projects maven configuration (pom.xml), maven is configured to copy the required database driver to the target.

<plugin> <groupId>io.openliberty.tools</groupId> <artifactId>liberty-maven-plugin</artifactId> <version>3.3.4</version> <configuration> <copyDependencies> <location>${project.build.directory}/wlp/lib</location> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> <!-- ... copy other dependencies --> </copyDependencies> </configuration> </plugin>

JPA PU injection example

To persist entities and to access persisted entities, an EntityManager can be injected into a bean. The name parameter of the ApplicationScoped annotation matches the name of the PU (Persistence Unit) configured in the persistence.xml

package org.whatever.app.boundary.repo; import org.whatever.app.entity.WhatEver; import javax.enterprise.context.ApplicationScoped; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @ApplicationScoped public class WhatEverRepo { @PersistenceContext(name = "jpa-unit") private EntityManager em; public WhatEver getWhatEver(int id) { return em.find(WhatEver.class, id); } // ... some other methods to access entities from the repo }

reference

Open Liberty docs Relational database connections with JDBC
IBM WebSphere Application Server Liberty docs Liberty: Directory locations and properties
Open Liberty docs Fileset (fileset)
Open Liberty docs securityUtility encode

A very helpful reference with an example pom.xml and server.xml from Lulseged Zerfu unfortunately is no longer online. I’m keeeping it here for the sake of completeness:

Open Liberty google group discussion about Open Liberty JPA setup (dead link)