Accessing and persisting data in microservices using Java Persistence API (JPA)

duration 20 minutes

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:

git clone https://github.com/openliberty/guide-jpa-intro.git
cd guide-jpa-intro

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 the Event class.
backendServices/src/main/java/io/openliberty/guides/event/models/Event.java

Event.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2018, 2022 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12package io.openliberty.guides.event.models;
 13
 14import java.io.Serializable;
 15import jakarta.persistence.Entity;
 16import jakarta.persistence.Table;
 17import jakarta.persistence.NamedQuery;
 18import jakarta.persistence.GeneratedValue;
 19import jakarta.persistence.Id;
 20import jakarta.persistence.Column;
 21import jakarta.persistence.GenerationType;
 22
 23// tag::Entity[]
 24@Entity
 25// end::Entity[]
 26// tag::Table[]
 27@Table(name = "Event")
 28// end::Table[]
 29// tag::NamedQuery[]
 30@NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e")
 31@NamedQuery(name = "Event.findEvent", query = "SELECT e FROM Event e WHERE "
 32    + "e.name = :name AND e.location = :location AND e.time = :time")
 33// end::NamedQuery[]
 34// tag::Event[]
 35public class Event implements Serializable {
 36    private static final long serialVersionUID = 1L;
 37
 38    // tag::GeneratedValue[]
 39    @GeneratedValue(strategy = GenerationType.AUTO)
 40    // end::GeneratedValue[]
 41    // tag::Id[]
 42    @Id
 43    // end::Id[]
 44    // tag::Column[]
 45    @Column(name = "eventId")
 46    // end::Column[]
 47    private int id;
 48
 49    @Column(name = "eventLocation")
 50    private String location;
 51    @Column(name = "eventTime")
 52    private String time;
 53    @Column(name = "eventName")
 54    private String name;
 55
 56    public Event() {
 57    }
 58
 59    public Event(String name, String location, String time) {
 60        this.name = name;
 61        this.location = location;
 62        this.time = time;
 63    }
 64
 65    public int getId() {
 66        return id;
 67    }
 68
 69    public void setId(int id) {
 70        this.id = id;
 71    }
 72
 73    public String getLocation() {
 74        return location;
 75    }
 76
 77    public void setLocation(String location) {
 78        this.location = location;
 79    }
 80
 81    public String getTime() {
 82        return time;
 83    }
 84
 85    public void setTime(String time) {
 86        this.time = time;
 87    }
 88
 89    public void setName(String name) {
 90        this.name = name;
 91    }
 92
 93    public String getName() {
 94        return name;
 95    }
 96
 97    @Override
 98    public int hashCode() {
 99        final int prime = 31;
100        int result = 1;
101        result = prime * result + id;
102        result = prime * result + ((location == null) ? 0 : location.hashCode());
103        result = prime * result + ((name == null) ? 0 : name.hashCode());
104        result = prime * result
105                 + (int) (serialVersionUID ^ (serialVersionUID >>> 32));
106        result = prime * result + ((time == null) ? 0 : time.hashCode());
107        return result;
108    }
109
110    @Override
111    public boolean equals(Object obj) {
112        if (this == obj) {
113            return true;
114        }
115        if (obj == null) {
116            return false;
117        }
118        if (getClass() != obj.getClass()) {
119            return false;
120        }
121        Event other = (Event) obj;
122        if (location == null) {
123            if (other.location != null) {
124                return false;
125            }
126        } else if (!location.equals(other.location)) {
127            return false;
128        }
129        if (time == null) {
130            if (other.time != null) {
131                return false;
132            }
133        } else if (!time.equals(other.time)) {
134            return false;
135        }
136        if (name == null) {
137            if (other.name != null) {
138                return false;
139            }
140        } else if (!name.equals(other.name)) {
141            return false;
142        }
143
144        return true;
145    }
146
147    @Override
148    public String toString() {
149        return "Event [name=" + name + ", location=" + location + ", time=" + time
150                + "]";
151    }
152}
153// end::Event[]

The following table breaks down the new annotations:

Annotation Description

@Entity

Declares the class as an entity

@Table

Specifies details of the table such as name

@NamedQuery

Specifies a predefined database query that is run by an EntityManager instance.

@Id

Declares the primary key of the entity

@GeneratedValue

Specifies the strategy used for generating the value of the primary key. The strategy = GenerationType.AUTO code indicates that the generation strategy is automatically selected

@Column

Specifies that the field is mapped to a column in the database table. The name attribute is optional and indicates the name of the column in the table

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  <library id="derbyJDBCLib">
21    <fileset dir="${shared.resource.dir}/" includes="derby*.jar" />
22  </library>
23
24  <!-- Datasource Configuration -->
25  <!-- tag::data-source[] -->
26  <dataSource id="eventjpadatasource" jndiName="jdbc/eventjpadatasource">
27    <jdbcDriver libraryRef="derbyJDBCLib" />
28    <properties.derby.embedded databaseName="EventDB" createDatabase="create" />
29  </dataSource>
30  <!-- end::data-source[] -->
31
32</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 the EventDao class.
backendServices/src/main/java/io/openliberty/guides/event/dao/EventDao.java

EventDao.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2018, 2022 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License 2.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-2.0/
 8 *
 9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11// end::copyright[]
12package io.openliberty.guides.event.dao;
13
14import java.util.List;
15import jakarta.persistence.EntityManager;
16import jakarta.persistence.PersistenceContext;
17
18import io.openliberty.guides.event.models.Event;
19
20import jakarta.enterprise.context.RequestScoped;
21
22@RequestScoped
23// tag::EventDao[]
24public class EventDao {
25
26    // tag::PersistenceContext[]
27    @PersistenceContext(name = "jpa-unit")
28    // end::PersistenceContext[]
29    private EntityManager em;
30
31    // tag::createEvent[]
32    public void createEvent(Event event) {
33        // tag::Persist[]
34        em.persist(event);
35        // end::Persist[]
36    }
37    // end::createEvent[]
38
39    // tag::readEvent[]
40    public Event readEvent(int eventId) {
41        // tag::Find[]
42        return em.find(Event.class, eventId);
43        // end::Find[]
44    }
45    // end::readEvent[]
46
47    // tag::updateEvent[]
48    public void updateEvent(Event event) {
49        // tag::Merge[]
50        em.merge(event);
51        // end::Merge[]
52    }
53    // end::updateEvent[]
54
55    // tag::deleteEvent[]
56    public void deleteEvent(Event event) {
57        // tag::Remove[]
58        em.remove(event);
59        // end::Remove[]
60    }
61    // end::deleteEvent[]
62
63    // tag::readAllEvents[]
64    public List<Event> readAllEvents() {
65        return em.createNamedQuery("Event.findAll", Event.class).getResultList();
66    }
67    // end::readAllEvents[]
68
69    // tag::findEvent[]
70    public List<Event> findEvent(String name, String location, String time) {
71        return em.createNamedQuery("Event.findEvent", Event.class)
72            .setParameter("name", name)
73            .setParameter("location", location)
74            .setParameter("time", time).getResultList();
75    }
76    // end::findEvent[]
77}
78// end::EventDao[]

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 the Event entity class to the data store by calling the persist() method on an EntityManager instance. The entity instance becomes managed and changes to it will be tracked by the entity manager.

  • The readEvent() method returns an instance of the Event entity class with the specified primary key by calling the find() method on an EntityManager 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 the Event entity class by using the Event.findAll query specified in the @NamedQuery annotation on the Event class. Similarly, the findEvent() method uses the Event.findEvent named query to find an event with the given name, location and time.

Event.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2018, 2022 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12package io.openliberty.guides.event.models;
 13
 14import java.io.Serializable;
 15import jakarta.persistence.Entity;
 16import jakarta.persistence.Table;
 17import jakarta.persistence.NamedQuery;
 18import jakarta.persistence.GeneratedValue;
 19import jakarta.persistence.Id;
 20import jakarta.persistence.Column;
 21import jakarta.persistence.GenerationType;
 22
 23// tag::Entity[]
 24@Entity
 25// end::Entity[]
 26// tag::Table[]
 27@Table(name = "Event")
 28// end::Table[]
 29// tag::NamedQuery[]
 30@NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e")
 31@NamedQuery(name = "Event.findEvent", query = "SELECT e FROM Event e WHERE "
 32    + "e.name = :name AND e.location = :location AND e.time = :time")
 33// end::NamedQuery[]
 34// tag::Event[]
 35public class Event implements Serializable {
 36    private static final long serialVersionUID = 1L;
 37
 38    // tag::GeneratedValue[]
 39    @GeneratedValue(strategy = GenerationType.AUTO)
 40    // end::GeneratedValue[]
 41    // tag::Id[]
 42    @Id
 43    // end::Id[]
 44    // tag::Column[]
 45    @Column(name = "eventId")
 46    // end::Column[]
 47    private int id;
 48
 49    @Column(name = "eventLocation")
 50    private String location;
 51    @Column(name = "eventTime")
 52    private String time;
 53    @Column(name = "eventName")
 54    private String name;
 55
 56    public Event() {
 57    }
 58
 59    public Event(String name, String location, String time) {
 60        this.name = name;
 61        this.location = location;
 62        this.time = time;
 63    }
 64
 65    public int getId() {
 66        return id;
 67    }
 68
 69    public void setId(int id) {
 70        this.id = id;
 71    }
 72
 73    public String getLocation() {
 74        return location;
 75    }
 76
 77    public void setLocation(String location) {
 78        this.location = location;
 79    }
 80
 81    public String getTime() {
 82        return time;
 83    }
 84
 85    public void setTime(String time) {
 86        this.time = time;
 87    }
 88
 89    public void setName(String name) {
 90        this.name = name;
 91    }
 92
 93    public String getName() {
 94        return name;
 95    }
 96
 97    @Override
 98    public int hashCode() {
 99        final int prime = 31;
100        int result = 1;
101        result = prime * result + id;
102        result = prime * result + ((location == null) ? 0 : location.hashCode());
103        result = prime * result + ((name == null) ? 0 : name.hashCode());
104        result = prime * result
105                 + (int) (serialVersionUID ^ (serialVersionUID >>> 32));
106        result = prime * result + ((time == null) ? 0 : time.hashCode());
107        return result;
108    }
109
110    @Override
111    public boolean equals(Object obj) {
112        if (this == obj) {
113            return true;
114        }
115        if (obj == null) {
116            return false;
117        }
118        if (getClass() != obj.getClass()) {
119            return false;
120        }
121        Event other = (Event) obj;
122        if (location == null) {
123            if (other.location != null) {
124                return false;
125            }
126        } else if (!location.equals(other.location)) {
127            return false;
128        }
129        if (time == null) {
130            if (other.time != null) {
131                return false;
132            }
133        } else if (!time.equals(other.time)) {
134            return false;
135        }
136        if (name == null) {
137            if (other.name != null) {
138                return false;
139            }
140        } else if (!name.equals(other.name)) {
141            return false;
142        }
143
144        return true;
145    }
146
147    @Override
148    public String toString() {
149        return "Event [name=" + name + ", location=" + location + ", time=" + time
150                + "]";
151    }
152}
153// end::Event[]
  • 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 the merge() method so that changes to loaded fields of the detached entity are tracked.

  • The deleteEvent() method removes an instance of the Event entity class from the database by calling the remove() method on an EntityManager 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

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2018, 2022 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12package io.openliberty.guides.event.resources;
 13
 14import jakarta.json.Json;
 15import jakarta.json.JsonArray;
 16import jakarta.json.JsonObject;
 17import jakarta.json.JsonArrayBuilder;
 18import jakarta.json.JsonObjectBuilder;
 19import jakarta.transaction.Transactional;
 20import jakarta.ws.rs.Consumes;
 21import jakarta.ws.rs.FormParam;
 22import jakarta.ws.rs.GET;
 23import jakarta.ws.rs.PUT;
 24import jakarta.ws.rs.POST;
 25import jakarta.ws.rs.DELETE;
 26import jakarta.ws.rs.Path;
 27import jakarta.ws.rs.PathParam;
 28import jakarta.ws.rs.Produces;
 29import jakarta.ws.rs.core.MediaType;
 30import jakarta.ws.rs.core.Response;
 31import jakarta.enterprise.context.RequestScoped;
 32import jakarta.inject.Inject;
 33
 34import io.openliberty.guides.event.dao.EventDao;
 35import io.openliberty.guides.event.models.Event;
 36
 37// tag::RequestedScoped[]
 38@RequestScoped
 39// end::RequestedScoped[]
 40@Path("events")
 41// tag::DAO[]
 42// tag::EventResource[]
 43public class EventResource {
 44
 45    @Inject
 46    private EventDao eventDAO;
 47
 48    /**
 49     * This method creates a new event from the submitted data (name, time and
 50     * location) by the user.
 51     */
 52    @POST
 53    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 54    // tag::Transactional[]
 55    @Transactional
 56    // end::Transactional[]
 57    public Response addNewEvent(@FormParam("name") String name,
 58        @FormParam("time") String time, @FormParam("location") String location) {
 59        Event newEvent = new Event(name, location, time);
 60        if (!eventDAO.findEvent(name, location, time).isEmpty()) {
 61            return Response.status(Response.Status.BAD_REQUEST)
 62                           .entity("Event already exists").build();
 63        }
 64        eventDAO.createEvent(newEvent);
 65        return Response.status(Response.Status.NO_CONTENT).build();
 66    }
 67
 68    /**
 69     * This method updates a new event from the submitted data (name, time and
 70     * location) by the user.
 71     */
 72    @PUT
 73    @Path("{id}")
 74    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
 75    @Transactional
 76    public Response updateEvent(@FormParam("name") String name,
 77        @FormParam("time") String time, @FormParam("location") String location,
 78        @PathParam("id") int id) {
 79        Event prevEvent = eventDAO.readEvent(id);
 80        if (prevEvent == null) {
 81            return Response.status(Response.Status.NOT_FOUND)
 82                           .entity("Event does not exist").build();
 83        }
 84        if (!eventDAO.findEvent(name, location, time).isEmpty()) {
 85            return Response.status(Response.Status.BAD_REQUEST)
 86                           .entity("Event already exists").build();
 87        }
 88        prevEvent.setName(name);
 89        prevEvent.setLocation(location);
 90        prevEvent.setTime(time);
 91
 92        eventDAO.updateEvent(prevEvent);
 93        return Response.status(Response.Status.NO_CONTENT).build();
 94    }
 95
 96    /**
 97     * This method deletes a specific existing/stored event
 98     */
 99    @DELETE
100    @Path("{id}")
101    @Transactional
102    public Response deleteEvent(@PathParam("id") int id) {
103        Event event = eventDAO.readEvent(id);
104        if (event == null) {
105            return Response.status(Response.Status.NOT_FOUND)
106                           .entity("Event does not exist").build();
107        }
108        eventDAO.deleteEvent(event);
109        return Response.status(Response.Status.NO_CONTENT).build();
110    }
111
112    /**
113     * This method returns a specific existing/stored event in Json format
114     */
115    @GET
116    @Path("{id}")
117    @Produces(MediaType.APPLICATION_JSON)
118    @Transactional
119    public JsonObject getEvent(@PathParam("id") int eventId) {
120        JsonObjectBuilder builder = Json.createObjectBuilder();
121        Event event = eventDAO.readEvent(eventId);
122        if (event != null) {
123            builder.add("name", event.getName()).add("time", event.getTime())
124                .add("location", event.getLocation()).add("id", event.getId());
125        }
126        return builder.build();
127    }
128
129    /**
130     * This method returns the existing/stored events in Json format
131     */
132    @GET
133    @Produces(MediaType.APPLICATION_JSON)
134    @Transactional
135    public JsonArray getEvents() {
136        JsonObjectBuilder builder = Json.createObjectBuilder();
137        JsonArrayBuilder finalArray = Json.createArrayBuilder();
138        for (Event event : eventDAO.readAllEvents()) {
139            builder.add("name", event.getName()).add("time", event.getTime())
140                   .add("location", event.getLocation()).add("id", event.getId());
141            finalArray.add(builder.build());
142        }
143        return finalArray.build();
144    }
145}
146// end::DAO[]
147// end::EventResource[]

Running the application

You started the Open Liberty in dev mode at the beginning of the guide, so all the changes were automatically picked up.

When Liberty is running, 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 the EventEntityIT class.
backendServices/src/test/java/it/io/openliberty/guides/event/EventEntityIT.java

EventEntityIT.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2018, 2022 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12package it.io.openliberty.guides.event;
 13
 14import static org.junit.jupiter.api.Assertions.assertEquals;
 15
 16import java.util.HashMap;
 17import jakarta.json.JsonObject;
 18import jakarta.ws.rs.client.ClientBuilder;
 19import jakarta.ws.rs.core.Form;
 20import jakarta.ws.rs.core.Response.Status;
 21
 22import org.junit.jupiter.api.AfterEach;
 23import org.junit.jupiter.api.BeforeAll;
 24import org.junit.jupiter.api.BeforeEach;
 25import org.junit.jupiter.api.Test;
 26import io.openliberty.guides.event.models.Event;
 27
 28public class EventEntityIT extends EventIT {
 29
 30    private static final String JSONFIELD_LOCATION = "location";
 31    private static final String JSONFIELD_NAME = "name";
 32    private static final String JSONFIELD_TIME = "time";
 33    private static final String EVENT_TIME = "12:00 PM, January 1 2018";
 34    private static final String EVENT_LOCATION = "IBM";
 35    private static final String EVENT_NAME = "JPA Guide";
 36    private static final String UPDATE_EVENT_TIME = "12:00 PM, February 1 2018";
 37    private static final String UPDATE_EVENT_LOCATION = "IBM Updated";
 38    private static final String UPDATE_EVENT_NAME = "JPA Guide Updated";
 39
 40    private static final int NO_CONTENT_CODE = Status.NO_CONTENT.getStatusCode();
 41    private static final int NOT_FOUND_CODE = Status.NOT_FOUND.getStatusCode();
 42
 43    @BeforeAll
 44    public static void oneTimeSetup() {
 45        port = System.getProperty("backend.http.port");
 46        baseUrl = "http://localhost:" + port + "/";
 47    }
 48
 49    @BeforeEach
 50    public void setup() {
 51        form = new Form();
 52        client = ClientBuilder.newClient();
 53
 54        eventForm = new HashMap<String, String>();
 55
 56        eventForm.put(JSONFIELD_NAME, EVENT_NAME);
 57        eventForm.put(JSONFIELD_LOCATION, EVENT_LOCATION);
 58        eventForm.put(JSONFIELD_TIME, EVENT_TIME);
 59    }
 60
 61    @Test
 62    // tag::testInvalidRead[]
 63    public void testInvalidRead() {
 64        assertEquals(true, getIndividualEvent(-1).isEmpty(),
 65          "Reading an event that does not exist should return an empty list");
 66    }
 67    // end::testInvalidRead[]
 68
 69    @Test
 70    // tag::testInvalidDelete[]
 71    public void testInvalidDelete() {
 72        int deleteResponse = deleteRequest(-1);
 73        assertEquals(NOT_FOUND_CODE, deleteResponse,
 74          "Trying to delete an event that does not exist should return the "
 75          + "HTTP response code " + NOT_FOUND_CODE);
 76    }
 77    // end::testInvalidDelete[]
 78
 79    @Test
 80    // tag::testInvalidUpdate[]
 81    public void testInvalidUpdate() {
 82        int updateResponse = updateRequest(eventForm, -1);
 83        assertEquals(NOT_FOUND_CODE, updateResponse,
 84          "Trying to update an event that does not exist should return the "
 85          + "HTTP response code " + NOT_FOUND_CODE);
 86    }
 87    // end::testInvalidUpdate[]
 88
 89    @Test
 90    // tag::testReadIndividualEvent[]
 91    public void testReadIndividualEvent() {
 92        int postResponse = postRequest(eventForm);
 93        assertEquals(NO_CONTENT_CODE, postResponse,
 94          "Creating an event should return the HTTP reponse code " + NO_CONTENT_CODE);
 95
 96        Event e = new Event(EVENT_NAME, EVENT_LOCATION, EVENT_TIME);
 97        JsonObject event = findEvent(e);
 98        event = getIndividualEvent(event.getInt("id"));
 99        assertData(event, EVENT_NAME, EVENT_LOCATION, EVENT_TIME);
100
101        int deleteResponse = deleteRequest(event.getInt("id"));
102        assertEquals(NO_CONTENT_CODE, deleteResponse,
103          "Deleting an event should return the HTTP response code " + NO_CONTENT_CODE);
104    }
105    // end::testReadIndividualEvent[]
106
107    @Test
108    // tag::testCURD[]
109    public void testCRUD() {
110        int eventCount = getRequest().size();
111        int postResponse = postRequest(eventForm);
112        assertEquals(NO_CONTENT_CODE, postResponse,
113          "Creating an event should return the HTTP reponse code " + NO_CONTENT_CODE);
114
115        Event e = new Event(EVENT_NAME, EVENT_LOCATION, EVENT_TIME);
116        JsonObject event = findEvent(e);
117        assertData(event, EVENT_NAME, EVENT_LOCATION, EVENT_TIME);
118
119        eventForm.put(JSONFIELD_NAME, UPDATE_EVENT_NAME);
120        eventForm.put(JSONFIELD_LOCATION, UPDATE_EVENT_LOCATION);
121        eventForm.put(JSONFIELD_TIME, UPDATE_EVENT_TIME);
122        int updateResponse = updateRequest(eventForm, event.getInt("id"));
123        assertEquals(NO_CONTENT_CODE, updateResponse,
124          "Updating an event should return the HTTP response code " + NO_CONTENT_CODE);
125
126        e = new Event(UPDATE_EVENT_NAME, UPDATE_EVENT_LOCATION, UPDATE_EVENT_TIME);
127        event = findEvent(e);
128        assertData(event, UPDATE_EVENT_NAME, UPDATE_EVENT_LOCATION, UPDATE_EVENT_TIME);
129
130        int deleteResponse = deleteRequest(event.getInt("id"));
131        assertEquals(NO_CONTENT_CODE, deleteResponse,
132          "Deleting an event should return the HTTP response code " + NO_CONTENT_CODE);
133        assertEquals(eventCount, getRequest().size(),
134          "Total number of events stored should be the same after testing "
135          + "CRUD operations.");
136    }
137    // end::testCURD[]
138
139    @AfterEach
140    public void teardown() {
141        response.close();
142        client.close();
143    }
144
145}

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

Copy file contents
Copied to clipboard

Prerequisites:

Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

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

Like Open Liberty? Star our repo on GitHub.

Star