Java Persistence with Spring Boot 3, Spring Data JPA with Hibernate, and the Oracle Database 23c Free — Developer Release

Juarez Junior
6 min readNov 13, 2023
Oracle Database 23c Free — Developer Release

by Juarez Junior

Introduction

A previous blog post has introduced the Oracle Database 23c Free — Developer Release, its new features and how to connect to it from a Java application with JDBC.

This blog post will explore another scenario with the Oracle Database 23c Free — Developer Release and a Spring Boot 3.0 application with Spring Data JPA.

Without further ado, let’s get started!

Prerequisites

Run an Oracle Database 23c Free — Developer Release instance on Docker

Assuming that you already know how to run an Oracle Database 23 Free instance on Docker as explained in a previous blog post, run the command below from the Windows Command Prompt (cmd):

docker run -d -p 1521:1521 -e ORACLE_PASSWORD=<your password> -v oracle-volume:/opt/oracle/oradata gvenzl/oracle-free

Replace <your password> above with your chosen password as required.

Note that you have also to prepare a directory called oradata to persist data with the oracle-volume as shown above.

If everything goes well, you can run the command docker ps -al to confirm that your Docker container is up and running with the Oracle Database 23c Free — Developer Release instance as expected.

Oracle Database 23c Free — Developer Edition — up and running

Connect to your DB 23C Free instance and run the DDL script

Now, you must connect to the DB 23C Free database instance and execute the DDL script to create the tables for our sample Spring Boot application.

Open the Oracle SQL Developer tool as explained previously, copy the DDL script from the Gist below — and execute it as required by clicking the Run Statement button.

Make sure the password for the user ORACLE_23C_USER follows Oracle’s requirements for DB passwords. Otherwise, an error will happen during the script execution process.

The Spring Boot application

It’s beyond the scope of this blog post to teach you what Spring Boot is about. There are plenty of tutorials on this subject, including the official Spring Boot 3.0 documentation and code samples.

Our main goal is to show you how to properly configure and allow connections from a Spring Boot application that uses the Oracle UCP (Oracle Connection Pool) as an alternative to HikariCP (default connection pool implementation in Spring Boot).

Nevertheless, we’ll provide an overview of the main components of the code samples to allow you to understand what you should expect it to do.

Basically, the main application components are Spring Boot 3, Spring Data JPA (Hibernate), and Oracle UCP (Oracle Connection Pool).

Code sample — application components

Employee.java

This class represents an entity that relates to the corresponding database table, as you can see below it has the required Entity annotation.

com.oracle.dev.jdbc.springboot3.jpa.ucp.Employee

Spring Boot 3 requires Spring Framework 6, which in turn requires Java 17 and Jakarta EE 9 and is compatible with the recently released Jakarta EE 10. You will note then that the Entity annotation now belongs to a package related to the Jakarta Persistence API — jakarta.persistence.Entity.

EmployeeApplication.java

This is the main entry point and class used to bootstrap and launch a Spring application from a Java main method.

com.oracle.dev.jdbc.springboot3.jpa.ucp.EmployeeApplication

EmployeeController.java

This is a class that adheres to the Controller design pattern and implements the REST endpoints for our sample application. Note that it is highlighted below the annotation that maps the HTTP GET requests to /employee that we’ll use to test the application.

com.oracle.dev.jdbc.springboot3.jpa.ucp.EmployeeController

EmployeeRepository.java

This interface implements the Repository design pattern by extending the org.springframework.data.jpa.repository.JpaRepository interface. It will help to expose CRUD operations for the specific domain object, Employee.java.

com.oracle.dev.jdbc.springboot3.jpa.ucp.EmployeeRepository

Configure the Spring (JDBC) Datasource to connect to your DB23c instance

Our code sample uses Spring Boot 3.0, which has a built-in mechanism for configuration using a file called application.properties. You can find the file under the src/main/resources folder.

Now it’s possible to use Oracle UCP Connection Pool as a native Spring Datasource. We’ll use the properties below for using Oracle UCP (Universal Connection Pool. Note that such properties require a Spring Boot version greater than 2.4.0.

Oracle’s UCP as a native Spring Datasource

First, we have to configure the connection URL, username, and password as shown below.

Spring Datasource connection details

We’ll use the following format for the spring.datasource.url parameter:

jdbc:oracle:thin@[hostname]:[port]/[DB service/name]

Then, provide a username and password as well.

Run the Spring Boot application locally

Change to the root directory of your project, and run the Maven command below as usual. Note the extra argument to the JVM, which is to allow the Byte Buddy component to run with JDK 21 during the Maven build process.

Run the commands below.

cd C:\java-projects\23c-springboot3-jpa-ucp
mvn clean package -Dnet.bytebuddy.experimental=true
cd target
java -jar 23c-springboot3-jpa-ucp-1.0-SNAPSHOT.jar

You will see some messages logged by Spring Boot 3.

Spring Boot 3 app started

Test the Spring Boot application from a browser

Provided that your application has started successfully as shown below.

Spring Boot 3 application started successfully

Open a web browser and access its URL http://localhost:8080/employees

If everything is working properly, you will see the database records returned as JSON as below.

DB records from the Spring Boot application

Test the Spring Boot application with httpie

You can also use the httpie tool to test it if you prefer.

Open the tool, then create the New draft option (plus sign +) as shown below, and provide the target URL http://localhost:8080/employees for a plain HTTP request with a GET HTTP method.

httpie — configure HTTP GET request

Now you can perform the same request from the tool. Just click the Send button and you will see the records returned from the application once again as shown below.

You will receive an HTTP 200 OK status code and the corresponding JSON response as the HTTP response body.

Debugging database apps — inspecting your Spring Data JPA/Hibernate queries

The application.properties file has some properties to allow you to inspect the Spring Data JPA/Hibernate queries so you can debug your applications if needed.

Spring Data JPA/Hibernate query — debugging enabled

If you enable them, you will also be able to see the executed query below.

Hibernate query

Wrapping it up

That’s it! You learned how to use Java Persistence with Spring Boot 3 along with the specific parameters for Spring Boot supported by the Oracle Universal Connection Pool, how to use JPA and Hibernate to create a database application that connects to Oracle Database 23c Free — Dev Release. I hope you enjoyed this blog post, so stay tuned!

References

Oracle Database 23c Free — Developer Release

Spring Boot 3

Spring Data JPA with Hibernate

Jakarta Persistence 3.1

Spring Framework 6.0

HikariCP - Connection Pool (default in Spring)

Oracle UCP (Universal Connection Pool) — Developer’s Guide

Develop Java applications with Oracle Database

Oracle SQL Developer

Oracle Developers and Oracle OCI Free Tier

Join our Oracle Developers channel on Slack to discuss Java, JDK, JDBC, GraalVM, Microservices with Spring Boot, Helidon, Quarkus, Micronaut, Reactive Streams, Cloud, DevOps, IaC, and other topics!

Build, test, and deploy your applications on Oracle Cloud — for free! Get access to OCI Cloud Free Tier!

--

--