Spring Data JPA+Spring Boot+MySQL

What is JPA?

JPA(Java Persistence API) is the standard specification for object-relational mapping between relation database tables and Java classes.

Popular JPA providers:

  • Hibernate: Most widely used and very well documented.
  • Oracle Toplink
  • EclipseLink: Open source Project from the Eclipse Foundation based on Toplink
  • Apache OpenJPA: Open source implementation for JPA. 
What is Spring Data JPA?

Implementing a data access layer of an application is very cumbersome. Spring Data JPA aims to significantly reduce boilerplate code needed for implementing Data Access Layer and also simplify the Data Access Layer, so that developer focus more on the business logic rather than wasting time and energy on building complex and cumbersome data access layers.

Please note that Spring Data JPA is not a JPA provider. Spring Data JPA requires JPA provider such as Hibernate or Eclipse Link etc.

Spring Data JPA is a Data Access Abstraction on top of JPA. It reduces the amount of boilerplate code required by JPA. That makes the implementation of the persistence layer easier and faster.

Please note that Spring Boot uses Hibernate as the default JPA Provider.

Spring JPA Data repository interfaces

Spring Data makes the process of working with entities a lot easier by merely defining repository interfaces.

These come with a set of pre-defined methods and allow the possibility of adding custom methods in each interface.

  • CrudRepository
  • PagingAndSortingRepository
  • JpaRepository
CrudRepository

CrudRepository provides create, read, update, and delete(CRUD) functions.

Screen Shot 2020-02-13 at 8.05.37 PM

PagingAndSortingRepository
  • extends CrudRepository interface.
  • It provides methods to do pagination and sort records

Screen Shot 2020-02-13 at 8.11.41 PM

JpaRepository
  • It extends PagingAndSortingRepository interface. It contains all APIs of CrudRepository and PagingAndSortingRepository.
  • provides JPA related methods such as flushing the persistence context and delete records in a batch

Screen Shot 2020-02-13 at 8.27.47 PM

Example

We will develop a very simple application for our example.

Technology Stack

  • Spring Framework – Spring core, Spring context
  • Spring Boot
  • Spring Data JPA
  • Hibernate – Default JPA provider used by Spring Boot
  • MySQL DB

Project Structure

Screen Shot 2020-02-13 at 6.39.16 PM

Define database.properties

Screen Shot 2020-02-13 at 6.39.49 PM

Create AppConfig 

  • Configure dataSource, entityManagerFactory and transactionManager beans.
  • We have used JavaConfig based repository configuration by using @EnableJpaRepositories.

Config

Create the Entity Class – Student

Screen Shot 2020-02-13 at 6.46.55 PM

Define the Repository Interface

You just need to create an interface and extend one of the below inbuilt Spring JPA Data interfaces.

  • CrudRepository
  • PagingAndSortingRepository
  • JpaRepository

In the below example, we have used CrudRepository.

Screen Shot 2020-02-13 at 6.47.12 PM

Spring Boot SpringApplication class

  • Use @SpringBootApplication annotation to specify the SpringApplication class.
  • It is used to bootstrap and launch a Spring application from a Java main method.

Screen Shot 2020-02-13 at 6.46.25 PM

Output

Before Operations:[Student [id=1, name=Gyan, age=18, weight=50], Student [id=2, name=Rochit, age=16, weight=55], Student [id=3, name=Virat, age=18, weight=47], Student [id=8, name=Ravi, age=17, weight=65], Student [id=9, name=Teja, age=14, weight=58], Student [id=11, name=Viku, age=16, weight=44], Student [id=12, name=Ajay, age=16, weight=44], Student [id=15, name=Ramu, age=18, weight=47], Student [id=20, name=Sachin, age=14, weight=50], Student [id=21, name=Ramesh, age=14, weight=50], Student [id=23, name=Prabhakar, age=14, weight=50]]
Student [id=1, name=Gyan, age=18, weight=50]
false
After Operations:[Student [id=1, name=Gyan, age=18, weight=70], Student [id=2, name=Rochit, age=16, weight=55], Student [id=8, name=Ravi, age=17, weight=65], Student [id=9, name=Teja, age=14, weight=58], Student [id=11, name=Viku, age=16, weight=44], Student [id=12, name=Ajay, age=16, weight=44], Student [id=15, name=Ramu, age=18, weight=47], Student [id=20, name=Sachin, age=14, weight=50], Student [id=21, name=Ramesh, age=14, weight=50], Student [id=23, name=Prabhakar, age=14, weight=50], Student [id=30, name=Mantu, age=15, weight=50]]

Leave a Reply

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