Spring Boot Rest JPA Hibernate Example

Here we are going to learn about Spring Boot Rest JPA Hibernate Example. Last article we have explained about Spring boot Hello World example

Technologies used:

  1. Java 1.8
  2. Spring boot 2.0.3
  3. JPA
  4. MYSQL
  5. Embedded Tomcat

What is Spring JPA?

Spring Data JPA API provides JpaTemplate class to integrate spring application with JPA.

JPA (Java Persistent API) is the sun specification for persisting objects in the enterprise application. It is currently used as the replacement for complex entity beans.

The implementation of JPA specification is provided by many vendors such as:

  • Hibernate
  • Toplink
  • iBatis
  • OpenJPA etc.

Spring Boot Rest JPA Hibernate Example:

Create the project :

Spring Boot provides a web tool called Spring Initializer to bootstrap an application quickly.


Spring Boot Rest JPA Hibernate Example
Spring Boot Rest JPA Hibernate Example

After all the details entered, Click Generate Project to Download the project and import As Maven to your favorite IDEs.

  • Project Structure:

Spring Boot JPA Hibernate Example
Spring Boot JPA Hibernate Example
  • Pom Dependencies:

POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project.


As explained out an earlier article, Spring Boot is AutoConfigurable no need of any Configuration. Spring Boot tries to auto-configure a DataSource if spring-data-jpa is in the classpath by reading the database configuration from application.properties file.

  • Spring Starter:

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes, as shown in the following below example.

Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their “application class”. @SpringBootApplication annotation can be used to enable those three features, that is:

  • @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism.
  • @ComponentScan: enable @Component scan on the package where the application is located (see the best practices).
  • @Configuration: allow to register extra beans in the context or import additional configuration classes.
  • Data model Class:

Create a new package called model inside com.javalog and add a class named User.java with following contents –

  • UserRepository:

Create a new package called repository inside com.javalog and add an interface named UserRepository.java and extend it from JpaRepository. This interface used to access the User data from the database.

It comes with a JpaRepository interface which defines methods for all the CRUD operations on the entity, and a default implementation of JpaRepository called SimpleJpaRepository.

  • UserController:

Here we are in final step. Create a new package called controller inside com.javalog.springbootjpaExample. This class contains a Rest API of Create and retrieving a User.

  • Running the Application:

We’ve successfully built all the APIs for our application. Let’s now run the app and test the APIs. The application will start at Spring Boot’s default tomcat port 8080.

Just go to the root directory of the application and type the following command to run it –

mvn spring-boot:run

  • Testing the APIs:

Creating new User by API: /api/add

Spring Boot Rest JPA Hibernate Example
Spring Boot Rest JPA Hibernate Example

Retrieving All the user by API : /api/all

Spring Boot Rest JPA Hibernate Example
Spring Boot Rest JPA Hibernate Example 

Related Posts: 


Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons