In this post, we will see how to create a custom repository in Spring Data JPA? We can create custom repository extending any of these interfaces. Each repository has it’s own benefits and power. You can choose according to your need.
Repository
CrudRepository
PagingAndSortingRepository
JpaRepository
QueryByExampleExecutor
Let’s see the first hierarchy of these repository interfaces.
Create a custom repository extending Repository interface. The Repository interface is a marker interface. It doesn’t have any predefined methods.
package com.javatute.repository; import java.io.Serializable; import org.springframework.data.repository.Repository; import com.javatute.entity.Student; public interface StudentRepository extends Repository<Student, Serializable> { }
Create a custom repository extending CrudRepository interface. The CrudRepository interface contains methods to perform CRUD operation. You get benefit to use those methods if you define your repository extending CrudRepository interface.
import java.io.Serializable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; import com.javatute.entity.Student; @Repository public interface StudentRepository extends CrudRepository<Student, Serializable> { }
See more about Repository vs CrudRepository here.
Create a custom repository extending PagingAndSortingRepository interface. The PagingAndSortingRepository interface contains methods to perform sorting and Pageable.
package com.javatute.repository; import java.io.Serializable; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; import com.javatute.entity.Student; @Repository public interface StudentRepository extends PagingAndSortingRepository<Student, Serializable> { }
Create a custom repository extending JpaRepository interface. The JpaRepository interface contains methods to perform a batch operation. you can use deleteAllInBacth(), saveAndFlush() methods if you define your repository extending JpaRepository.
package com.javatute.repository; import java.io.Serializable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.javatute.entity.Student; @Repository public interface StudentRepository extends JpaRepository<Student, Serializable> { }
See more about Repository vs CrudRepository here.
That’s all about how to create a custom repository in Spring Data JPA.
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 tutorials.
- @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.
Summary – We have seen Difference Difference between Repository and CrudRepository in Spring Data JPA.
We have seen Spring Boot custom repository.