DEV Community

Cover image for Spring Boot + Oracle example: Build a CRUD app
Tien Nguyen
Tien Nguyen

Posted on • Updated on

Spring Boot + Oracle example: Build a CRUD app

In this tutorial, we're gonna build a Spring Boot and Oracle example with database connection that uses Hibernate, Spring Data JPA to make CRUD Operations. You'll know:

  • How to configure Spring Data, JPA, Hibernate to work with Oracle Database
  • How to define Data Models and Repository interfaces
  • Way to create Spring Rest Controller to process HTTP requests
  • Way to use Spring Data JPA to interact with Oracle Database

Full Article at: https://bezkoder.com/spring-boot-hibernate-oracle/

Overview of Spring Boot and Oracle example

We will build a Spring Boot + Hibernate + Oracle example that exports Rest CRUD API for a Tutorial application:

  • Each Tutotial has id, title, description, published status.
  • Apis help to create, retrieve, update, delete Tutorials.
  • Apis also support custom finder methods such as find by published status or by title.

These are APIs that we need to provide:

Methods Urls Actions
POST /api/tutorials create new Tutorial
GET /api/tutorials retrieve all Tutorials
GET /api/tutorials/:id retrieve a Tutorial by :id
PUT /api/tutorials/:id update a Tutorial by :id
DELETE /api/tutorials/:id delete a Tutorial by :id
DELETE /api/tutorials delete all Tutorials
GET /api/tutorials/published find all published Tutorials
GET /api/tutorials?title=[keyword] find all Tutorials which title contains keyword
  • We make CRUD operations & finder methods with Hibernate and Spring Data JPA's JpaRepository.
  • The database will be Oracle 12c/19c by configuring project dependency & datasource.

When you run the Spring Boot + Oracle example, tutorials table will be automatically generated in Oracle Database.
You can see things like this:

spring-boot-oracle-example-database-table

Create some Tutorials:

spring-boot-oracle-example-crud-create-tutorial

Check Oracle database after create operation:

spring-boot-oracle-example-crud-database-create-tutorial

Update some Tutorials:

spring-boot-oracle-example-crud-update-tutorial

Check Oracle database after update operation:

spring-boot-oracle-example-crud-database-update-tutorial

Get all Tutorials:

spring-boot-oracle-example-crud-retrieve-tutorial

Get a Tutorial by Id:

spring-boot-oracle-example-crud-retrieve-one-tutorial

Find all published Tutorials:

spring-boot-oracle-example-crud-search-tutorial-active

Find all Tutorials which title contains 'ring':

spring-boot-oracle-example-crud-search-tutorial-title

Delete a Tutorial:

spring-boot-oracle-example-crud-delete-tutorial

Check Oracle database after delete Operation:

spring-boot-oracle-example-crud-database-delete-tutorial

Delete all Tutorials:

spring-boot-oracle-example-crud-delete-all-tutorial

Now the table has no record:

spring-boot-oracle-example-crud-database-delete-all-tutorial

You can also test this Spring Boot App with Client in one of these posts:

Technology

  • Java 8
  • Spring Boot 2 (with Spring Web MVC, Spring Data JPA)
  • Oracle 12c or 19c
  • Maven 3.6.1

Project Structure

spring-boot-oracle-example-project-structure

Let me explain it briefly.

Tutorial data model class corresponds to entity and table tutorials.
TutorialRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in TutorialController.
TutorialController is a RestController which has request mapping methods for RESTful requests such as: getAllTutorials, createTutorial, updateTutorial, deleteTutorial, findByPublished...
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
pom.xml contains dependencies for Spring Boot and Oracle.

For step by step and Github, please visit:
https://bezkoder.com/spring-boot-hibernate-oracle/

Further Reading

Spring Boot One To Many example with Spring JPA, Hibernate

If you want to add Pagination to this Spring project, you can find the instruction at:
Spring Boot Pagination & Filter example | Spring JPA, Pageable

To sort/order by multiple fields:
Spring Data JPA Sort/Order by multiple Columns | Spring Boot

Handle Exception for this Rest APIs is necessary:

Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest

More Practice:

Top comments (0)