Spring Boot + Oracle example: Build a CRUD app
Tags: spring, oracle, java, webdev
Published: 2021-05-20 05:02:30

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:
<ul>
    	<li>How to configure Spring Data, JPA, Hibernate to work with Oracle Database</li>
    	<li>How to define Data Models and Repository interfaces</li>
	<li>Way to create Spring Rest Controller to process HTTP requests</li>
   	<li>Way to use Spring Data JPA to interact with Oracle Database</li>
</ul>

Full Article at: [https://bezkoder.com/spring-boot-hibernate-oracle/](https://bezkoder.com/spring-boot-hibernate-oracle/)

<h2>Overview of Spring Boot and Oracle example</h2>
We will build a Spring Boot + Hibernate + Oracle example that exports Rest CRUD API for a Tutorial application:
<ul>
	<li>Each Tutotial has id, title, description, published status.</li>
	<li>Apis help to create, retrieve, update, delete Tutorials.</li>
	<li>Apis also support custom finder methods such as find by published status or by title.</li>
</ul>

These are APIs that we need to provide:
<div class="table-responsive">
<table class="table table-hover  table table-hover" width="100%">
<thead>
<tr>
<th>Methods</th>
<th>Urls</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>POST</td>
<td>/api/tutorials</td>
<td>create new Tutorial</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials</td>
<td>retrieve all Tutorials</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials/:id</td>
<td>retrieve a Tutorial by <code>:id</code></td>
</tr>
<tr>
<td>PUT</td>
<td>/api/tutorials/:id</td>
<td>update a Tutorial by <code>:id</code></td>
</tr>
<tr>
<td>DELETE</td>
<td>/api/tutorials/:id</td>
<td>delete a Tutorial by <code>:id</code></td>
</tr>
<tr>
<td>DELETE</td>
<td>/api/tutorials</td>
<td>delete all Tutorials</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials/published</td>
<td>find all published Tutorials</td>
</tr>
<tr>
<td>GET</td>
<td>/api/tutorials?title=[keyword]</td>
<td>find all Tutorials which title contains <code>keyword</code></td>
</tr>
</tbody>
</table>
</div>
- We make CRUD operations & finder methods with Hibernate and Spring Data JPA's <code>JpaRepository</code>.
- The database will be Oracle 12c/19c by configuring project dependency & datasource.

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

![spring-boot-oracle-example-database-table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kzo9ywb1skwmid4ldh75.png)

Create some Tutorials:

![spring-boot-oracle-example-crud-create-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dffyo2kup4hlmk9c10vw.png)

Check Oracle database after create operation:

![spring-boot-oracle-example-crud-database-create-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxujs4st4h79uuhxg8mw.png)

Update some Tutorials:

![spring-boot-oracle-example-crud-update-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/puf1ewdah5y7mxm11is7.png)

Check Oracle database after update operation:

![spring-boot-oracle-example-crud-database-update-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtevrg87d05ybgikfqmi.png)

Get all Tutorials:

![spring-boot-oracle-example-crud-retrieve-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zk30sr91owi2pedviany.png)

Get a Tutorial by Id:

![spring-boot-oracle-example-crud-retrieve-one-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cunnf1n4fq2b2uko94fs.png)

Find all <em><strong>published</strong></em> Tutorials:

![spring-boot-oracle-example-crud-search-tutorial-active](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02m6atkpezs5iogy8yb6.png)

Find all Tutorials which title contains 'ring':

![spring-boot-oracle-example-crud-search-tutorial-title](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wk53i5zzydc7bwy55k2c.png)

Delete a Tutorial:

![spring-boot-oracle-example-crud-delete-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flj0ngtwev2xt2o7d0b1.png)

Check Oracle database after delete Operation:

![spring-boot-oracle-example-crud-database-delete-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ucpsnnoxkajn8ewcftk.png)

Delete all Tutorials:

![spring-boot-oracle-example-crud-delete-all-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w1pzjrxfkql003y5kyj.png)

Now the table has no record:

![spring-boot-oracle-example-crud-database-delete-all-tutorial](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fjnpupux06xnuze8lgr.png)

You can also test this Spring Boot App with Client in one of these posts:
<ul>
	<li><a href="https://bezkoder.com/angular-crud-app/">Angular 8 CRUD Application example with Web API</a></li>
	<li><a href="https://bezkoder.com/angular-10-crud-app/">Angular 10 CRUD Application example with Web API</a></li>
	<li><a href="https://bezkoder.com/angular-11-crud-app/">Angular 11 CRUD Application example with Web API</a></li>
	<li><a href="https://bezkoder.com/vue-js-crud-app/">Vue 2 CRUD Application with Vue Router & Axios</a></li>
	<li><a href="https://bezkoder.com/vue-3-crud/">Vue 3 CRUD Application with Axios & Vue Router</a></li>
	<li><a href="https://bezkoder.com/react-crud-web-api/">React CRUD example to consume Web API</a></li>
	<li><a href="https://bezkoder.com/react-redux-crud-example/">React Redux CRUD example with API calls</a></li>
</ul>

<h2>Technology</h2>
<ul>
    	<li>Java 8</li>
    	<li>Spring Boot 2 (with Spring Web MVC, Spring Data JPA)</li>
    	<li>Oracle 12c or 19c</li>
    	<li>Maven 3.6.1</li>
</ul>

<h2>Project Structure</h2>
![spring-boot-oracle-example-project-structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ygmbkwywj36jxgi070p6.png)

Let me explain it briefly.

– <code>Tutorial</code> data model class corresponds to entity and table <em>tutorials</em>.
– <code>TutorialRepository</code> is an interface that extends <a href="https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html">JpaRepository</a> for CRUD methods and custom finder methods. It will be autowired in <code>TutorialController</code>.
– <code>TutorialController</code> is a <a href="https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html">RestController</a> which has request mapping methods for RESTful requests such as: <em>getAllTutorials</em>, <em>createTutorial</em>, <em>updateTutorial</em>, <em>deleteTutorial</em>, <em>findByPublished</em>...
– Configuration for Spring Datasource, JPA & Hibernate in <strong>application.properties</strong>.
– <strong>pom.xml</strong> contains dependencies for Spring Boot and Oracle.

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

## Further Reading
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)

If you want to add Pagination to this Spring project, you can find the instruction at:
<a href="https://bezkoder.com/spring-boot-pagination-filter-jpa-pageable/">Spring Boot Pagination & Filter example | Spring JPA, Pageable</a>

To sort/order by multiple fields:
<a href="https://bezkoder.com/spring-data-sort-multiple-columns/">Spring Data JPA Sort/Order by multiple Columns | Spring Boot</a>

Handle Exception for this Rest APIs is necessary:
- <a href="https://bezkoder.com/spring-boot-controlleradvice-exceptionhandler/">Spring Boot @ControllerAdvice & @ExceptionHandler example</a>
- <a href="https://bezkoder.com/spring-boot-restcontrolleradvice/">@RestControllerAdvice example in Spring Boot</a>

Or way to write Unit Test for the JPA Repository:
<a href="https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/">Spring Boot Unit Test for JPA Repositiory with @DataJpaTest</a>

More Practice:
- <a href="https://bezkoder.com/spring-boot-rest-xml/">Spring Boot Rest XML example – Web service with XML Response</a>
- <a href="https://bezkoder.com/spring-boot-file-upload/">Spring Boot Multipart File upload example</a>
- <a href="https://bezkoder.com/spring-boot-pagination-sorting-example/">Spring Boot Pagination and Sorting example</a>