Spring Data with Spring Boot
This blog will show how to integrate Spring Data with Spring Boot. The flow will be from the Rest Controller which will wire the service to make call to JPA Repository as shown below.
Initialize the Spring Boot Application using start.spring.io with Web and JPA dependency added to it.
Need to add below dependency to support Spring Data JPA and embedded h2 database
- spring-boot-starter-data-jpa [ org.springframework.boot ]
- h2 [ com.h2database ]
Define an interface that extends JpaRepository for the entity along with the primary key. Here, we have an Employee Entity and the primary key data type for the entity is String.
Inject the Repo as dependency in Service Layer and make use of findAll() method and getById method to return the list of employees or return a specific employee
Define a model, that maps entity bean to a table.
Note : Please add @JsonIgnoreProperties to override below exception
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serialiser found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
Need to add below annotation
Place the SQL files in main/resources folder to load it into DB , on application startup
H2 Console Dashboard
We can able to access the H2 dashboard @ localhost:8095/h2-console ( Here the port no specified in application.properties should match )
And the JDBC URL should be : jdbc:h2:mem:testdb
Then click on test connection, to see its successful.
Test the endpoint through Postman
Happy Learning !