Skip to Content

Adding persistence to your app using JPA (Java Persistence API)

0 %
Adding persistence to your app using JPA (Java Persistence API)
// Explore More Tutorials

Adding persistence to your app using JPA (Java Persistence API)


Adding persistence to your app using JPA (Java Persistence API)

You will learn

In this tutorial you will implement a simple domain model and implement the corresponding persistence layer using JPA (Java Persistence API). The domain model only features a single class: FavoriteCity, so that we can bookmark or favorite our favorite cities.

Step 1: Create a base class

Let’s create a base class for our domain model first. That’s usually considered best practices as it provides us with a central place to add common functionality to be shared across all domain model objects on later. For that purpose, select New > Class from the context menu entry on the weatherapp project and provide the following details:

  • Package name:
  • Classname: BaseObject
Adding a Java Class
Step 2: Replace code

Replace the contents of the file with this code from GitHub and save your changes.

Step 3: Create another Java class

Next, create another Java class ( using the same procedure:

  • Package name:
  • Classname: FavoriteCity
Specifying the package and class name
Step 4: Replace the code

Replace the contents of the file with this code from GitHub and save your changes.

Step 5: Create a configuration file

Next, we need to create a configuration file for our persistence layer. By Maven conventions, these non-source code artifacts should be located in a separate source code folder called: src/main/resources. Hence, let’s create that source folder via the corresponding context menu entry on the Java Resources node in the Project Explorer: New > Source Folder. Provide the following information:

  • Project name: weatherapp
  • Folder name: src/main/resources
Creating a Java resources directory
Step 6: Create new folder

Open the context menu of this newly created source folder and choose the New > Other option and then select the Folder option. Name the new folder META-INF (all capitals!) and click on Finish.

Step 7: Create persistence XML file

Open the context menu of the newly created META-INF folder and select New > File. Name the new file persistence.xml and click on Finish.

Creating a configuration file for persistence
Step 8: Edit the file

Copy and paste the following XML content into the persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"

<persistence-unit name="application" transaction-type="RESOURCE_LOCAL">
      <property name="eclipselink.ddl-generation" value="create-tables"/>
Updating the persistence.xml file
Step 9: Add dependencies

Next, we need to add some more dependencies to our pom.xml file. In this case, the most important dependency is on EclipseLink (our JPA implementation of choice). However, we also need to declare dependencies for the Derby DB and Jackson (a serialization framework needed to convert data into JSON and vice versa.)

<!-- EclipseLink (and JPA) -->
 <!-- Derby -->


Adding dependencies to pom.xml for persistence
Step 10: Add Jackson version

We also need to add the Jackson version as a property in the properties section of the pom.xml file (as we have done in the previous section) and save your changes.

adding persistence property to pom.xml
Step 11: Create CRUD service

Next step is to create the respective CRUD service. For that purpose, create a new class with the following details:

  • Package name:
  • Classname: FavoriteCityService
Adding a new CRUD service
Step 12: Replace the code

Replace the contents of the file with this code from GitHub and save your changes.

Step 13: Register REST service

To register our RESTful service implementation in the web.xml configuration file, add the fully qualified classname of our FavoriteCityService class to the comma-separated list of jaxrs.serviceClasses. See the snippet below for where to enter the fully qualified classname inside the <param-value> element (don’t forget the comma at the end of the AuthenticationService line).

Updating web.xml for persistence
Step 14: Add initialization parameter for JSON de-serialization

Just below the aforementioned <init-param> tag we need to add another <init-param> for JSON de-serialization as follows:

Adding init-param for JSON de-serialization
Step 15: Define a Data Source

The last change to make is to define a DataSource within the web.xml in order to connect to the underlying database. To do this, copy and paste the following XML snippet after the closing </welcome-file-list> tag:

defining the DataSource in the web.xml file
Step 16: Replace code

In order to properly test our RESTful service we need a REST tool (e.g. Postman) that allows you to execute HTTP calls in a convenient manner.

Step 17: Provide credentials

Within Postman, enter http://localhost:8080/weatherapp/api/v1/cities in the URL input field and make sure to provide your username/password as Basic Auth parameters in the Authorization tab.

Afterwards, make sure to update the request by pressing the respective Update request button. That will then add the “Authorization” parameter as an HTTP header parameter to your request.

Using a REST client to connect to the Java app
Step 18: Execute the call

Once you execute the call, you’ll see two empty brackets “[]” (indicating an empty array) after successful authentication. Don’t worry, we haven’t saved any cities as favorites yet, so that’s just what we would expect.

Server response to REST client

Next Steps

Back to top