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 JDBCdid not work
in a simple maven project, run with mvn liberty:run:
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
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.
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
}
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: