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

Details

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:

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

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.

Step 8: Edit the file

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

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.)

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.

<org.codehaus.jackson-version>1.9.9</org.codehaus.jackson-version>

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:com.sap.hana.cloud.samples.weatherapp.api

Classname:FavoriteCityService

Step 12: Replace the code

Replace the contents of the FavoriteCityService.java 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).

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:

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.

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.