In this tutorial we will take a look into spring data jpa in detail. We will see integratation between spring data and spring boot with examples. We will perform different crud operations using spring data and check out different ways and advantages of using it. We will have some REST endpoints exposed through spring controller and we will be invoking these endpoints to perform different crud operations. We will also take a look into how to make use of entitymanager and hibernate session in spring data. If you are looking for spring boot hibernate example then visit here - spring boot hibernate example
Spring data simplifies the data access operations to a very greater extent. The most compelling feature of spring data is the ability to create repository implementations automatically, at runtime, from a repository interface. Hence, you only define the dao interface and the operations you want to perform such as save, read, delete and spring data will provide the implementations at runtime. Not only this, it also provides many inbuilt features for paginations, querying, auditing etc. Spring data provides support for both relational and NoSql DB.
Among the many modules provided by Spring Data such as Spring Data Solr, Spring Data MongoDB, Spring Data REST to deal with the persistence layer, Spring Data JPA is also a sub project to deal with persisitence layer. Spring data provides enhanced support for JPA based implementations. We only require to define our repository interfaces, including custom finder methods, and Spring will provide the implementation automatically at run-time.
Lets discuss about different interfaces provided by spring data JPA to deal with persistence layer.
Repository - It is the central interface in the spring data repository abstraction. This is defined as a marker interface.
CrudRepository - CrudRepository provides methods such as save(), findOne(), findAll(), delete() etc. for the CRUD operations. This interface extends the Repository interface. While creating our dao interface we extend this interface with our custom interface and just invoke the abstract methods defined in CrudRepository and spring data will take care of providing the corresponding implementations at run-time.
JpaRepository - It is a JPA specific extension of Repository. It extends CrudRepository and provides some extra JPA related method such as flushing the persistence context and delete record in a batch.
PagingAndSortingRepository - It also extends CrudRepository provide methods to do pagination and sorting records.
Let us first define the project structure.
spring-boot-starter-parent: It provides useful Maven defaults. It also provides a dependency-management section so that you can omit version tags for existing dependencies.
spring-boot-starter-web: It includes all the dependencies required to create a web app. This will avoid lining up different spring common project versions.
spring-boot-starter-tomcat: It enable an embedded Apache Tomcat 7 instance, by default.This can be also marked as provided if you wish to deploy the war to any other standalone tomcat.
spring-boot-starter-tomcat: It provides key dependencies for Hibernate, Spring Data JPA and Spring ORM.
@SpringBootApplication enables many defaults. It is a convenience annotation that adds @Configuration, @EnableAutoConfiguration, @EnableWebMvc, @ComponentScan
main() method uses Spring Boot SpringApplication.run() method to launch an application.
Following is our sample datasource configuration. Spring boot automatically identifies the driver class name with the help of datasource url.
Following is the entity class.UserDetails.java
Following is the controller class. It exposes rest endpoints for our crud operations.
Following is the service class that acts as a bridge between controller and dao.
The UserDao interface extends
CrudRepository which has different crud methods such as create, findOne, delete etc and hence our UserDao automatically inherits them which is available for our service class to use. Its spring data which will generate the implementations at run time for these crud methods. Hence, we dont have to provide the implementations.
Notice the generic parameters in CrudRepository. Based on these parameters, Spring data will perform different crud operations at run time on our behalf.
Apart from the different default methods defined in the CrudRepository, we have defined our own method -
findByEmail(). We have one parameter defined in our entiry class as email and by the naming conventions as findByEmail, Spring data will automatically identify that this is a search operation by email of the user.
Spring data is not limited to use of only JPA and some standards methods. Even it is possible to define some custom methods and custom operations too. You can also make use of JPA provided entitymanager to perform custom data manipulation as well as use hibernate session to make advanatage of different functions provided by hibernate.
Following is the implementation that uses entitymanager to save data in db while using spring data.
As we discussed, Spring data is not only limited to use of JPA, you can also make use of hibernate session and make use of different functions provided by hibernate.
Following are some sample DML. We will be creating some dummy user details using following insert statements.
1. Run Application.java as a java application.+
2. Now hit the url - localhost:8080/user/list and you can see following.
3. You can also try for post request using postman or ARC as follow:
Similarly you can make other requests.
I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.
Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.
©2017, DevGlan. All Rights Reserved.
Contact us: firstname.lastname@example.org