Codementor Events

Learn Spring Data JPA

Published Jun 27, 2023
Learn Spring Data JPA

JPA (Java Persistent API) is the java specification for persisting and storing objects also know as Object Relational Mapping (ORM) in enterprise application object-relational mappings and for managing persistent objects.

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

Hibernate
Toplink
iBatis
OpenJPA
each vendor implements the specification in different ways, which makes it harder for the developer to switch between implemented technologies.

A typical enterprise application is not limited to data is not relational anymore, now we need to store graphs, document stores, key-value stores, relational and non-relational databases, map-reduce frameworks, cloud-based data services, and columns. we have lots of different types of databases for different types of data which gave birth to Polyglot Persistence.

Spring Data is spring way to provide a consistent, Spring-based java persist API for data access while hiding the boilerplate code.

Spring Data is one of the core projects of the spring umbrella ecosystem. it's an abstract layer between the application and underlying database, it reduces the data access layer complexity and object-relational mappings while maintaining a rich and full-featured set of functionality.

Features

predefined CRUD operations
Powerful repository and custom object-mapping abstractions
integration via JavaConfig and custom XML namespaces
Support QueryDSL and JPA queries
batch loading, sorting, dynamical queries
Reduce boilerplate code and No-code Repositories
DynamicGenerated queries
Paging and sorting
Core concepts
The main interface in the Spring Data JPA is Repository. its a marker interface. It takes the domain or entity class and primary key type as arguments.

AppleRepository <-JpaRepository <-PagingAndSortingRepository <-CrudRepository <-Repository

each repository interface provides some additional abstract methods.

Repository

The Repository interface is the core and base repository in Spring Data JPA. All Spring Data Jpa interface has to directly or indirectly extend the Repository interface. its a marker interface which takes domain or entity class and its primary key type as arguments. it has no method.

CrudRepository

CrudRepository implements the base Repository interface and provides the method for all the basic CRUD operations for the entity class.

save
findById
existsById
findAll
findAllById
deleteById
delete
deleteAll
deleteAll
PagingAndSortingRepository

PagingAndSortingRepository defined in Spring Data JPA extends the CrudRepository. PagingAndSortingRepository provides predefined methods for retrieving the entity class using pagination and sorting.

findAll(Sort sort)
findAll(Pageable pageable)
JpaRepository

JpaRepository the interface is the topmost interface which extends PagingAndSortingRepository. in most of the case JpaRepository is used to take advantage of Spring Data JPA. JpaRepository doesn't provide any new method on its own but overrides methods provided in PagingAndSortingRepository, CrudRepository, Repository interface.

How Spring Data Jpa Internally Works
Spring Data Jpa provides JpaRepository, PagingAndSortingRepository, CrudRepository, Repository interface. Most of the general-purpose methods are predefined in these interfaces. but in java interface just declare methods, an implementing or base class is required which implements all the abstract methods, Spring Data Jpa is no exception.

Spring Data JPA provides JpaRepositoryImplementation interface which extends JpaRepository repository. SimpleJpaRepository class implements the the JpaRepositoryImplementation and provides all the implementations of Spring Data Jpa methods.

https://wesome.org/spring-data-jpa

Discover and read more posts from we some
get started
post commentsBe the first to share your opinion
Show more replies