git clone https://github.com/openliberty/guide-jpa-intro.git
cd guide-jpa-intro
Accessing and persisting data in microservices using Java Persistence API (JPA)
Prerequisites:
Learn how to use Java Persistence API (JPA) to access and persist data to a database for your microservices.
What you’ll learn
You will learn how to use the Java Persistence API (JPA) to map Java objects to relational database tables and perform create, read, update and delete (CRUD) operations on the data in your microservices.
JPA is a Jakarta EE specification for representing relational database table data as Plain Old Java Objects (POJO). JPA simplifies object-relational mapping (ORM) by using annotations to map Java objects to tables in a relational database. In addition to providing an efficient API for performing CRUD operations, JPA also reduces the burden of having to write JDBC and SQL code when performing database operations and takes care of database vendor-specific differences. This capability allows you to focus on the business logic of your application instead of wasting time implementing repetitive CRUD logic.
The application that you will be working with is an event manager, which is composed of a UI and an event microservice for creating, retrieving, updating, and deleting events. In this guide, you will be focused on the event microservice. The event microservice consists of a JPA entity class whose fields will be persisted to a database. The database logic is implemented in a Data Access Object (DAO) to isolate the database operations from the rest of the service. This DAO accesses and persists JPA entities to the database and can be injected and consumed by other components in the microservice. An Embedded Derby database is used as a data store for all the events.
You will use JPA annotations to define an entity class whose fields are persisted to the database. The interaction between your service and the database is mediated by the persistence context that is managed by an entity manager. In a Jakarta EE environment, you can use an application-managed entity manager or a container-managed entity manager. In this guide, you will use a container-managed entity manager that is injected into the DAO so Liberty manages the opening and closing of the entity manager for you.
Getting started
The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:
The start
directory contains the starting project that you will build upon.
The finish
directory contains the finished project that you will build.
Before you begin, make sure you have all the necessary prerequisites.
Try what you’ll build
The finish
directory in the root of this guide contains the finished application. Give it a try before you proceed.
To try out the application, run the following commands to navigate to the finish/frontendUI
directory and deploy the frontendUI
service to Open Liberty:
cd finish/frontendUI mvn liberty:run
Open another command-line session and run the following commands to navigate to the finish/backendServices
directory and deploy the service to Open Liberty:
cd finish/backendServices mvn liberty:run
After you see the following message in both command-line sessions, both your services are ready.
The defaultServer server is ready to run a smarter planet.
Point your browser to the http://localhost:9090/eventmanager.jsf URL to view the Event Manager application.
The event application does not display any events because no events are stored in the database. Go ahead and click Create Event
, located in the left navigation bar. After entering an event name, location and time, click Submit
to persist your event entity to the database. The event is now stored in the database and is visible in the list of current events.
Notice that if you stop the Open Liberty instance and then restart it, the events created are still displayed in the list of current events. Ensure you are in the finish/backendServices
directory and run the following Maven goals to stop and then restart the instance:
mvn liberty:stop
mvn liberty:run
The events created are still displayed in the list of current events. The Update
action link located beside each event allows you to make modifications to the persisted entity and the Delete
action link allows you to remove entities from the database.
After you are finished checking out the application, stop the Open Liberty instances by pressing CTRL+C
in the command-line sessions where you ran the backendServices
and frontendUI
services. Alternatively, you can run the liberty:stop
goal from the finish
directory in another command-line session for the frontendUI
and backendServices
services:
mvn -pl frontendUI liberty:stop mvn -pl backendServices liberty:stop
Defining a JPA entity class
Navigate to the start
directory to begin.
When you run Open Liberty in dev mode, dev mode listens for file changes and automatically recompiles and deploys your updates whenever you save a new change.
Run the following commands to navigate to the frontendUI
directory and start the frontendUI
service in dev mode:
cd frontendUI mvn liberty:dev
Open another command-line session and run the following commands to navigate to the backendServices
directory and start the service in dev mode:
cd backendServices mvn liberty:dev
After you see the following message, your Liberty instance is ready in dev mode:
************************************************************** * Liberty is running in dev mode.
Dev mode holds your command line to listen for file changes. Open another command-line session to continue, or open the project in your editor.
To store Java objects in a database, you must define a JPA entity class. A JPA entity is a Java object whose non-transient and non-static fields will be persisted to the database. Any Plain Old Java Object (POJO) class can be designated as a JPA entity. However, the class must be annotated with the @Entity
annotation, must not be declared final and must have a public or protected non-argument constructor. JPA maps an entity type to a database table and persisted instances will be represented as rows in the table.
The Event
class is a data model that represents events in the event microservice and is annotated with JPA annotations.
Create theEvent
class.backendServices/src/main/java/io/openliberty/guides/event/models/Event.java
Event.java
The following table breaks down the new annotations:
Annotation | Description |
---|---|
|
Declares the class as an entity |
|
Specifies details of the table such as name |
|
Specifies a predefined database query that is run by an |
|
Declares the primary key of the entity |
|
Specifies the strategy used for generating the value of the primary key. The |
|
Specifies that the field is mapped to a column in the database table. The |
Configuring JPA
The persistence.xml
file is a configuration file that defines a persistence unit. The persistence unit specifies configuration information for the entity manager.
Create the configuration file.
backendServices/src/main/resources/META-INF/persistence.xml
persistence.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<persistence version="2.2"
3 xmlns="http://xmlns.jcp.org/xml/ns/persistence"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
6 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
7 <!-- tag::persistence-unit[] -->
8 <!-- tag::transaction-type[] -->
9 <persistence-unit name="jpa-unit" transaction-type="JTA">
10 <!-- end::transaction-type[] -->
11 <!-- tag::jta-data[] -->
12 <jta-data-source>jdbc/eventjpadatasource</jta-data-source>
13 <!-- end::jta-data[] -->
14 <properties>
15 <!-- tag::schema-generation[] -->
16 <property name="jakarta.persistence.schema-generation.database.action"
17 value="create"/>
18 <property name="jakarta.persistence.schema-generation.scripts.action"
19 value="create"/>
20 <property name="jakarta.persistence.schema-generation.scripts.create-target"
21 value="createDDL.ddl"/>
22 <!-- end::schema-generation[] -->
23 </properties>
24 </persistence-unit>
25 <!-- end::persistence-unit[] -->
26</persistence>
The persistence unit is defined by the persistence-unit
XML element. The name
attribute is required and is used to identify the persistent unit when using the @PersistenceContext
annotation to inject the entity manager later in this guide. The transaction-type="JTA"
attribute specifies to use Java Transaction API (JTA) transaction management. Because of using a container-managed entity manager, JTA transactions must be used.
A JTA transaction type requires a JTA data source to be provided. The jta-data-source
element specifies the Java Naming and Directory Interface (JNDI) name of the data source that is used. The data source
has already been configured for you in the backendServices/src/main/liberty/config/server.xml
file. This data source configuration is where the Java Database Connectivity (JDBC) connection is defined along with some database vendor-specific properties.
server.xml
1<server description="Sample Liberty server">
2
3 <featureManager>
4 <feature>restfulWS-3.1</feature>
5 <feature>jsonb-3.0</feature>
6 <feature>jsonp-2.1</feature>
7 <feature>cdi-4.0</feature>
8 <feature>persistence-3.1</feature>
9 </featureManager>
10
11 <variable name="http.port" defaultValue="5050" />
12 <variable name="https.port" defaultValue="5051" />
13
14 <httpEndpoint httpPort="${http.port}" httpsPort="${https.port}"
15 id="defaultHttpEndpoint" host="*" />
16
17 <application location="backendServices.war" type="war" context-root="/"></application>
18
19 <!-- Derby Library Configuration -->
20 <!-- tag::shared-dir[] -->
21 <library id="derbyJDBCLib">
22 <fileset dir="${shared.resource.dir}/" includes="derby*.jar" />
23 </library>
24 <!-- end::shared-dir[] -->
25
26 <!-- Datasource Configuration -->
27 <!-- tag::data-source[] -->
28 <dataSource id="eventjpadatasource" jndiName="jdbc/eventjpadatasource">
29 <jdbcDriver libraryRef="derbyJDBCLib" />
30 <properties.derby.embedded databaseName="EventDB" createDatabase="create" />
31 </dataSource>
32 <!-- end::data-source[] -->
33
34</server>
The jakarta.persistence.schema-generation
properties are used here so that you aren’t required to manually create a database table to run this sample application. To learn more about the JPA schema generation and available properties, see Schema Generation, Section 9.4 of the JPA Specification
Performing CRUD operations using JPA
The CRUD operations are defined in the DAO. To perform these operations by using JPA, you need an EventDao
class.
Create theEventDao
class.backendServices/src/main/java/io/openliberty/guides/event/dao/EventDao.java
EventDao.java
To use the entity manager at runtime, inject it into the CDI bean through the @PersistenceContext
annotation. The entity manager interacts with the persistence context. Every EntityManager
instance is associated with a persistence context. The persistence context manages a set of entities and is aware of the different states that an entity can have. The persistence context synchronizes with the database when a transaction commits.
The EventDao
class has a method for each CRUD operation, so let’s break them down:
-
The
createEvent()
method persists an instance of theEvent
entity class to the data store by calling thepersist()
method on anEntityManager
instance. The entity instance becomes managed and changes to it will be tracked by the entity manager. -
The
readEvent()
method returns an instance of theEvent
entity class with the specified primary key by calling thefind()
method on anEntityManager
instance. If the event instance is found, it is returned in a managed state, but, if the event instance is not found,null
is returned. -
The
readAllEvents()
method demonstrates an alternative way to retrieve event objects from the database. This method returns a list of instances of theEvent
entity class by using theEvent.findAll
query specified in the@NamedQuery
annotation on theEvent
class. Similarly, thefindEvent()
method uses theEvent.findEvent
named query to find an event with the given name, location and time.
Event.java
-
The
updateEvent()
method creates a managed instance of a detached entity instance. The entity manager automatically tracks all managed entity objects in its persistence context for changes and synchronizes them with the database. However, if an entity becomes detached, you must merge that entity into the persistence context by calling themerge()
method so that changes to loaded fields of the detached entity are tracked. -
The
deleteEvent()
method removes an instance of theEvent
entity class from the database by calling theremove()
method on anEntityManager
instance. The state of the entity is changed to removed and is removed from the database upon transaction commit.
The DAO is injected into the backendServices/src/main/java/io/openliberty/guides/event/resources/EventResource.java
class and used to access and persist data. The @Transactional
annotation is used in the EventResource
class to declaratively control the transaction boundaries on the @RequestScoped
CDI bean. This ensures that the methods run within the boundaries of an active global transaction, which is why it is not necessary to explicitly begin, commit or rollback transactions. At the end of the transactional method invocation, the transaction commits and the persistence context flushes any changes to Event entity instances it is managing to the database.
EventResource.java
Configuring the Derby driver and the Liberty Maven plugin
To use a Derby database, you need to download its libraries and store them to the Liberty shared resources directory. Configure the Liberty Maven plug-in in the pom.xml
file of the backendServices
service.
Replace thebackendServices/pom.xml
configuration file.backendServices/pom.xml
backendServices/pom.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>io.openliberty.guides</groupId>
8 <artifactId>backendServices</artifactId>
9 <packaging>war</packaging>
10 <version>1.0-SNAPSHOT</version>
11
12 <properties>
13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15 <maven.compiler.source>21</maven.compiler.source>
16 <maven.compiler.target>21</maven.compiler.target>
17 <!-- Liberty configuration -->
18 <backend.service.http.port>5050</backend.service.http.port>
19 <backend.service.https.port>5051</backend.service.https.port>
20 </properties>
21
22 <dependencies>
23 <!-- Provided dependencies -->
24 <dependency>
25 <groupId>jakarta.platform</groupId>
26 <artifactId>jakarta.jakartaee-web-api</artifactId>
27 <version>10.0.0</version>
28 <scope>provided</scope>
29 </dependency>
30 <dependency>
31 <groupId>org.eclipse.microprofile</groupId>
32 <artifactId>microprofile</artifactId>
33 <version>6.1</version>
34 <type>pom</type>
35 <scope>provided</scope>
36 </dependency>
37 <!-- For tests -->
38 <dependency>
39 <groupId>org.junit.jupiter</groupId>
40 <artifactId>junit-jupiter-engine</artifactId>
41 <version>5.11.3</version>
42 <scope>test</scope>
43 </dependency>
44 <dependency>
45 <groupId>org.jboss.resteasy</groupId>
46 <artifactId>resteasy-client</artifactId>
47 <version>6.2.10.Final</version>
48 <scope>test</scope>
49 </dependency>
50 <dependency>
51 <groupId>org.jboss.resteasy</groupId>
52 <artifactId>resteasy-json-binding-provider</artifactId>
53 <version>6.2.10.Final</version>
54 <scope>test</scope>
55 </dependency>
56 <dependency>
57 <groupId>org.glassfish</groupId>
58 <artifactId>jakarta.json</artifactId>
59 <version>2.0.1</version>
60 <scope>test</scope>
61 </dependency>
62 <!-- Derby from https://mvnrepository.com/artifact/org.apache.derby/derby -->
63 <!-- tag::dependencies[] -->
64 <dependency>
65 <groupId>org.apache.derby</groupId>
66 <artifactId>derby</artifactId>
67 <version>10.17.1.0</version>
68 <scope>provided</scope>
69 </dependency>
70 <dependency>
71 <groupId>org.apache.derby</groupId>
72 <artifactId>derbyshared</artifactId>
73 <version>10.17.1.0</version>
74 <scope>provided</scope>
75 </dependency>
76 <dependency>
77 <groupId>org.apache.derby</groupId>
78 <artifactId>derbytools</artifactId>
79 <version>10.17.1.0</version>
80 <scope>provided</scope>
81 </dependency>
82 <!-- end::dependencies[] -->
83 </dependencies>
84 <build>
85 <finalName>${project.artifactId}</finalName>
86 <plugins>
87 <plugin>
88 <groupId>org.apache.maven.plugins</groupId>
89 <artifactId>maven-war-plugin</artifactId>
90 <version>3.4.0</version>
91 </plugin>
92 <!-- Enable liberty-maven plugin -->
93 <plugin>
94 <groupId>io.openliberty.tools</groupId>
95 <artifactId>liberty-maven-plugin</artifactId>
96 <version>3.11.1</version>
97 <configuration>
98 <!-- tag::copyDependencies[] -->
99 <copyDependencies>
100 <!-- tag::location[] -->
101 <location>${project.build.directory}/liberty/wlp/usr/shared/resources</location>
102 <!-- end::location[] -->
103 <dependency>
104 <groupId>org.apache.derby</groupId>
105 <artifactId>derby</artifactId>
106 </dependency>
107 <dependency>
108 <groupId>org.apache.derby</groupId>
109 <artifactId>derbyshared</artifactId>
110 </dependency>
111 <dependency>
112 <groupId>org.apache.derby</groupId>
113 <artifactId>derbytools</artifactId>
114 </dependency>
115 </copyDependencies>
116 <!-- end::copyDependencies[] -->
117 </configuration>
118 </plugin>
119 <!-- Plugin to run unit tests -->
120 <plugin>
121 <groupId>org.apache.maven.plugins</groupId>
122 <artifactId>maven-surefire-plugin</artifactId>
123 <version>3.5.1</version>
124 </plugin>
125 <!-- Plugin to run integration tests -->
126 <plugin>
127 <groupId>org.apache.maven.plugins</groupId>
128 <artifactId>maven-failsafe-plugin</artifactId>
129 <version>3.5.1</version>
130 <configuration>
131 <systemPropertyVariables>
132 <backend.http.port>${backend.service.http.port}</backend.http.port>
133 </systemPropertyVariables>
134 </configuration>
135 </plugin>
136 </plugins>
137 </build>
138</project>
server.xml
1<server description="Sample Liberty server">
2
3 <featureManager>
4 <feature>restfulWS-3.1</feature>
5 <feature>jsonb-3.0</feature>
6 <feature>jsonp-2.1</feature>
7 <feature>cdi-4.0</feature>
8 <feature>persistence-3.1</feature>
9 </featureManager>
10
11 <variable name="http.port" defaultValue="5050" />
12 <variable name="https.port" defaultValue="5051" />
13
14 <httpEndpoint httpPort="${http.port}" httpsPort="${https.port}"
15 id="defaultHttpEndpoint" host="*" />
16
17 <application location="backendServices.war" type="war" context-root="/"></application>
18
19 <!-- Derby Library Configuration -->
20 <!-- tag::shared-dir[] -->
21 <library id="derbyJDBCLib">
22 <fileset dir="${shared.resource.dir}/" includes="derby*.jar" />
23 </library>
24 <!-- end::shared-dir[] -->
25
26 <!-- Datasource Configuration -->
27 <!-- tag::data-source[] -->
28 <dataSource id="eventjpadatasource" jndiName="jdbc/eventjpadatasource">
29 <jdbcDriver libraryRef="derbyJDBCLib" />
30 <properties.derby.embedded databaseName="EventDB" createDatabase="create" />
31 </dataSource>
32 <!-- end::data-source[] -->
33
34</server>
This configuration adds three required Derby dependencies to the dependencies
configuration so Maven can download the Derby libraries locally. The copyDependencies
configuration instructs the Liberty Maven plug-in to copy the Derby libraries to the Liberty shared resources directory that is specified through the location
configuration, and is referenced in the derbyJDBCLib
library
configuration of the server.xml
file.
In the terminal where you started the backendServices
microservice, type r
and press the enter/return
key to restart the Liberty instance and pick up the Derby libraries.
Running the application
After you see the following message, your Liberty instance is ready in dev mode:
************************************************************** * Liberty is running in dev mode.
Go to the http://localhost:9090/eventmanager.jsf URL to view the Event Manager application.
Click Create Event
in the left navigation bar to create events that are persisted to the database. After you create an event, it is available to view, update, and delete in the Current Events
section.
Testing the application
Create theEventEntityIT
class.backendServices/src/test/java/it/io/openliberty/guides/event/EventEntityIT.java
EventEntityIT.java
The testInvalidRead()
, testInvalidDelete()
and testInvalidUpdate()
methods use a primary key that is not in the database to test reading, updating and deleting an event that does not exist, respectively.
The testReadIndividualEvent()
method persists a test event to the database and retrieves the event object from the database using the primary key of the entity.
The testCRUD()
method creates a test event and persists it to the database. The event object is then retrieved from the database to verify that the test event was actually persisted. Next, the name, location, and time of the test event are updated. The event object is retrieved from the database to verify that the updated event is stored. Finally, the updated test event is deleted and one final check is done to ensure that the updated test event is no longer stored in the database.
Running the tests
Since you started Open Liberty in dev mode, press the enter/return
key in the command-line session where you started the backendServices
service to run the tests for the backendServices
.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.event.EventEntityIT
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.703 sec - in it.io.openliberty.guides.event.EventEntityIT
Results :
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
When you are done checking out the services, exit dev mode by pressing CTRL+C
in the command-line sessions where you ran the frontendUI
and backendServices
services.
Great work! You’re done!
You learned how to map Java objects to database tables by defining a JPA entity class whose instances are represented as rows in the table. You have injected a container-managed entity manager into a DAO and learned how to perform CRUD operations in your microservice in Open Liberty.
Guide Attribution
Accessing and persisting data in microservices using Java Persistence API (JPA) by Open Liberty is licensed under CC BY-ND 4.0
Prerequisites:
Nice work! Where to next?
What did you think of this guide?
Thank you for your feedback!
What could make this guide better?
Raise an issue to share feedback
Create a pull request to contribute to this guide
Need help?
Ask a question on Stack Overflow