Spring Boot + JPA + Hibernate + Oracle

In this tutorial we show how to create a Spring Boot application that communicates with an Oracle data source through Hibernate.

Prerequisites:

  • Eclipse IDE (neon release)
  • Maven 4
  • Java 1.8

1- Create maven project

Open eclipse, then create a new maven project and name it as SpringBootHibernate.

At the end of this tutorial, we’ll get the following project structure:

2- pom.xml

Configure Spring Boot inside pom.xml through adding the following parent dependency:

Then add spring-boot-starter dependency in order to run our application as a standalone jar application:

Now in order to make use of Spring data jpa and hibernate, we need to just add spring-boot-starter-data-jpa as a dependency:

As soon as we include Spring Boot Starter JPA in our project, we get the following features from a wide variety of dependencies:

  • Auto-configuration of an in-memory embedded database which allows you to run your application without even setting up a database.
  • Auto-import of JPA API and Hibernate, adding this dependency will automatically import JPA API and use Hibernate as the default implementation.
  • Auto-read of the data source and hibernate configuration from application.properties.
  • Auto-creation of the entities as tables and auto execution of import.sql.

This is the whole pom.xml for reference:

3- Add oracle driver to classpath

In this tutorial, we’re going to override the default in-memory database provided by Spring Boot and use our own oracle database.

For this purpose we add “oracle-ojdbc6-11.2.0.3.jar” under WEB-INF/lib and define it in our classpath.

4- application.properties

Configure oracle data source and hibernate in application.properties:

5- Entities

Our entities represent a player and a team with a one to many relationship, each team could have many players while a player could only play with a single team at a time.

So we create our entities under com.programmer.gate.model package:

Since we set spring.jpa.hibernate.ddl-auto=create-drop inside application.properties, our application will automatically create Player and Team entities in our database along with their sequences and constraints.

Our application would also look for import.sql in the classpath and executes it if found.

In our example, we define import.sql under src/main/resources in order to fill our tables with static data:

6- Repositories

We define our repositories interfaces under com.programmer.gate.repository. Each repository extends Spring CrudRepository which provides a default implementation for the basic find,save and delete methods, so that we don’t care about defining implementation classes for them.

7- Service

Now we define our service class which holds the business logic of our application, our service exposes 2 methods: getAllTeamPlayers() and addBarcelonaPlayer() ( just rename it to your favorite club if you don’t like Barcelona :D), our service layer communicates directly with the repository layer.

8- Application.java

The final step is to create the Spring Boot initializer, this is the entry point of our application. We define Application.java under com.programmer.gate.

P.S: it’s worth to mention that Spring Boot application automatically reads and creates entities, repositories and services defined in the same or in a sub-package relative to where you have your initializer class, so if we define Application.java under a different package then we need to explicitly specify the package of model, repository and service.

Output:

When running the application as a standard java application, we get the following output in the console.

9- Source code

You can download the source code from this repository: spring-boot-jpa-hibernate

 

husseinterek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

3 Comments on "Spring Boot + JPA + Hibernate + Oracle"

avatar
newest oldest most voted
Shihad
Guest

Hi Hussein,

Thanks for the tutorial. I am facing some issues setting up the SpringBoot application. I have used same code and dependencies as yours, but while running the main class I am getting class not found exception for WebMvcConfigurer class. Then I added spring-boot-starter-web dependency and afterwards lot of similar class file not found issues came and I have to add each dependency.

I was still not able to succesfully set up the application. Could you please help. Only change I made from your code is the use of MySQL database and the latest starter parent version 2.0.1.RELEASE.

Regards,
Shihad

Tom Wu
Guest

The title of this tutorial would be more accurate as “Spring Boot + Spring Data JPA + Hibernate + Oracle” because it uses Spring Data JPA but not directly use Hibernate JPA.

Hibernate itself has JPA implementation (on top of its native Hibernate) and Hibernate JPA implementation also works smoothly with Spring Boot WITHOUT Spring Data JPA.

john
Guest

HELP!
2018-05-16 12:08:30 DEBUG org.hibernate.SQL – select team0_.id as id1_1_0_, team0_.name as name2_1_0_, players1_.team_id as team_id5_0_1_, players1_.id as id1_0_1_, players1_.id as id1_0_2_, players1_.name as name2_0_2_, players1_.num as num3_0_2_, players1_.position as position4_0_2_, players1_.team_id as team_id5_0_2_ from team team0_ left outer join player players1_ on team0_.id=players1_.team_id where team0_.id=?
2018-05-16 12:08:30 DEBUG org.hibernate.SQL – select player_seq.nextval from dual
2018-05-16 12:08:30 WARN o.h.e.jdbc.spi.SqlExceptionHelper – SQL Error: 2289, SQLState: 42000
2018-05-16 12:08:30 ERROR o.h.e.jdbc.spi.SqlExceptionHelper – ORA-02289: no existe la secuencia

2018-05-16 12:08:30 INFO o.s.b.a.l.AutoConfigurationReportLoggingInitializer –