Skip to Content

End-to-End Weather App Scenario Part 6

By akula86

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.


  1. 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
  2. Replace the contents of the file with this code from GitHub and save your changes.

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

    • Package name:
    • Classname: FavoriteCity
    Specifying the package and class name
  4. Replace the contents of the file with this code from GitHub and save your changes.

  5. 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
  6. 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.

  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. Replace the contents of the file with this code from GitHub and save your changes.

  13. 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
  14. 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
  15. 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
  16. 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.

  17. 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
  18. 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

Updated 12/22/2016

Time to Complete

20 min



Back to top