In this scenario you will learn more about developing Spring Boot applications using the Red Hat Runtimes platform. We will be building a simple Spring Boot application with a Database Persistence layer backed by Spring Data JPA and Hibernate.
In this scenario you learned about Spring Boot and how it can be used to access relational data from a Database. We will add additional scenarios that explain more how to build Spring Boot applications on OpenShift shortly, so check back to learn.openshift.com
To summarize: you started by adding spring-boot-data-jpa to the
pom.xml which brought in the Spring dependencies necessary to talk to a database through the JPA protocol from the Red Hat Maven Repository (since we used the BOM from Snowdrop). Then you added a JPA-annotated Model to represent the table, a Repository interface through which you queried the data, and an
import.sql file to seed the database with test data. Finally, we deployed everything to OpenShift.
Spring Boot is a powerful and easy to use framework for developing everything from monolithic applications to microservices, but Spring Boot becomes every better when we package it in a container and use an orchestrated platform like OpenShift. When using OpenShift we can, in a very consistent and fast way, deploy our Spring application to multiple environments without worry of environment differences. OpenShift also enables things like Deployment Pipelines and Blue/Green deployments but that was not covered in this scenario.
More background and related information on Red Hat OpenShift Application Runtimes and Eclipse Vert.x can be found here:
- Red Hat OpenShift Application Runtimes for Developers - Here you can get started with a project using different boosters and clone that project to your local computer. This also enables you to deploy your application on your own private OpenShift Container Platform or use OpenShift Online that is provided for free from Red Hat.
- Project Snowdrop homepage - This site has a lot of details of the work that Red Hat is doing to make Spring work great on Kubernetes and OpenShift.
Spring Boot Database Access with Spring Data JPA and Hibernate
Import the code
Let's refresh the code we'll be using. Run the following command to clone the sample project:
cd /root/projects && rm -rf rhoar-getting-started && git clone https://github.com/openshift-katacoda/rhoar-getting-started && cd rhoar-getting-started/spring/spring-db-access
Review the base structure of the application
For your convenience, this scenario has been created with a base project using the Java programming language and the Apache Maven build tool. Initially the project contains a couple web-oriented files which are out of scope for this module. See the Spring Rest Services module for more details. These files are in place to give a graphical view of the Database content.
As you review the content you will notice that there are a couple TODO comments. Do not remove them! These comments are used as markers for later exercises in this scenario.
In the next section we will add the dependencies and write the class files necessary to run the application.
Inspect Java runtime
An appropriate Java runtime has been installed for you. Ensure you can use it by running this command:
The command should report the version in use, for example (the versions and dates may be slightly different than the below example):
openjdk 11.0.10 2021-01-19 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)
If the command fails, wait a few moments and try again (it is installed in a background process and make take a few moments depending on system load).