Spring Data JPA is one of Spring Data modules which provides different API to perform CRUD operation. Let’s see some points which will help to understand Spring data JPA and its benefit.
Some important Pre Defined interfaces and classes of Spring Data JPA.
The Repository is a core interface, which is a marker interface.
public interface Repository<T, ID extends Serializable> { }
The CrudRepository interface extends Repository interface, contains the following method.
-
- save(S entity) – Used to save a single entity at a time.
- Iterable<S> save(Iterable<S> entities) – we can save multiple entities at a time.
- findOne(ID id) – use to get one entity basis of id.
- boolean exists(ID id) – used to check whether an entity is already exited in DB for given Id.
- Iterable<T> findAll() – find all entity of paricular type.
- Iterable<T> findAll(Iterable<ID> ids) – return all entity of given ids.
- long count() – returns the number of entities
- void delete(ID id) – delete the entity on basis of id
- void delete(T entity) – delete the entity which one we are passing.
- void delete(Iterable<? extends T> entities) – delete multiple entities which we are passing.
- void deleteAll() – delete all entities.
PagingAndSortingRepository extends CrudRepository, contains the following methods.
-
- Iterable<T> findAll(Sort sort) – Returns all entities sorted by the given options.
- Page<T> findAll(Pageable pageable) – Returns a Page of entities basis of paging restriction provided in the object.
JpaRepository extends PagingAndSortingRepository interface, contains following methods.
-
- List<T> findAll().
- List<T> findAll(Sort sort).
- List<T> findAll(Iterable<ID> ids).
- <S extends T> List<S> save(Iterable<S> entities).
- void flush().
- <S extends T> S saveAndFlush(S entity).
- void deleteInBatch(Iterable<T> entities).
- void deleteAllInBatch().
- T getOne(ID id).
We have seen important interfaces and its method. Let’s see what are the benefits of Spring Data JPA.
Benefits of Spring Data JPA – The Spring Data JPA is mainly used to the development of the repository layer. In Spring Data we can define the repository interfaces and methods to retrieve the data.
Consider we have an entity called Student.java and we have some records in the database as below.
Student.java
package com.javatute.entity; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "name") private String name; @Column(name = "roll_number") private String rollNumber; @Column(name = "university") String university; }
No need to write a query, we can define a custom method to retrieve the data. For example, suppose we have a table book and entity Student.java. we have a field name in the entity and we want to get data on the basis of name. we can define a method in StudentRepository something like below –
public interface StudentRepository extends JpaRepository { public List<Students> findByName(); }
Since CrudRepository provides predefined methods and we can define query methods(for accessing data) which reduce code size and Using Spring Data Jpa we can remove a lot of code by creating an abstract repository class that provides CRUD operations for our entities.
We can complex query using @Query annotation in Spring Data JPA. See an example here.
We can also write JPQL and Native Query with entity using @NamedQuery and @NamedNativeQuery annotation. See an Example here.
Spring Data Jpa provides PagingAndSortingRepository interface for sorting and pagination support. See an Example here.
Spring Data Jpa provides Named Parameters interface. See an Example here.
That’s all about Spring data JPA and its benefit.
You may like.
- Spring Data JPA greater than Example
- Spring Data JPA less than Example
- Spring Data JPA IsNull Example Using Spring Boot
- Spring Data findById() Vs getOne()
- Spring Data JPA CrudRepository findById()
- Spring Data JPA JpaRepository getOne()
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data CrudRepository existsById()
- Spring Data JPA delete() vs deleteInBatch()
- Spring Data JPA deleteAll() Vs deleteAllInBatch()
- Spring Data JPA JpaRepository deleteAllInBatch()
- Spring Data JPA deleteInBatch() Example
- Spring Data JPA JpaRepository saveAndFlush() Example
- Spring Data JPA CrudRepository count() Example
- Spring Data JPA CrudRepository delete() and deleteAll()
- Spring Data JPA CrudRepository deleteById() Example
- CrudRepository findAllById() Example Using Spring Boot
- Spring Data CrudRepository save() Method.
- Sorting in Spring Data JPA using Spring Boot.
- Spring Data JPA example using spring boot.
- Spring Data JPA and its benefit.
Other Spring Data JPA and Hibernate post.
- @Version Annotation Example In Hibernate.
- Hibernate Validator Constraints Example Using Spring Boot.
- @Temporal Annotation Example In Hibernate/Jpa Using Spring Boot.
- Hibernate Table Per Concrete Class Spring Boot.
- Hibernate Table Per Subclass Inheritance Spring Boot.
- Hibernate Single Table Inheritance using Spring Boot.
- One To One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle.
- One To One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle.
- One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.
- Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And
Spring Data JPA Docs.