In this tutorial, we will learn how to create a Spring Boot application that communicates with an Oracle data source through Hibernate.
Prerequisites
- Eclipse IDE
- Maven 4
- Java 1.8
Create a Maven Project
Open Eclipse, then create a new Maven project and name it SpringBootHibernate.
At the end of this tutorial, we’ll get the following project structure:
pom.xml
Configure Spring Boot inside your pom.xml by adding the following parent dependency:
Then add a spring-boot-starter dependency in order to run the 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 the JPA API and Hibernate. Adding this dependency will automatically import the 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:
Add the Oracle Driver to the 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.
application.properties
Configure the Oracle data source and Hibernate in application.properties:
Entities
Our entities represent a player and a team with a one-to-many relationship. Each team could have many players, whereas a player could only play with a single team at a time.
So we create our entities under the com.programmer.gate.model package:
Player.java:
Team.java:
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 execute it, if found.
In our example, we define import.sql under src/main/resources in order to fill our tables with static data:
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 we don’t care about defining implementation classes for them.
PlayerRepository:
TeamRepository:
Service
Now we define our service class, which holds the business logic of our application. Our service exposes two methods: getAllTeamPlayers() and addBarcelonaPlayer() ( just rename it to your favorite club if you don’t like Barcelona!). Our service layer communicates directly with the repository layer.
SoccerService.java:
SoccerServiceImpl:
Application.java
The final step is to create the Spring Boot initializer. This is the entry point of our application. We define Application.javaunder com.programmer.gate:
P.S.: It’s worth mentioning that the Spring Boot application automatically reads and creates entities, repositories, and services defined in the same package 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 the model, repository, and service.
Output:
When running the application as a standard Java app, we get the following output in the console:
No comments:
Post a Comment