📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
What Spring Data JPA?
Problem
CustomerDAOImpl
Solution
Spring Data Commons and Spring Data JPA Repositories/interfaces
Let's quickly discuss important repositories or interfaces of Spring Data Commons and Spring Data JPA.- Repository<T, ID extends Serializable> interface
- CrudRepository<T, ID extends Serializable> interface
- PagingAndSortingRepository<T, ID extends Serializable> interface
- QueryDslPredicateExecutor interface
The Repository<T, ID extends Serializable> interface
- It captures the type of the managed entity and the type of the entity’s id.
- It helps the Spring container to discover the “concrete” repository interfaces during classpath scanning.
package org.springframework.data.repository;
import org.springframework.stereotype.Indexed;
@Indexed
public interface Repository<T, ID> {
}
The CrudRepository<T, ID extends Serializable> interface
package org.springframework.data.repository;
import java.util.Optional;
@NoRepositoryBean
public interface CrudRepository < T, ID > extends Repository < T, ID > {
<S extends T > S save(S entity);
<S extends T > Iterable < S > saveAll(Iterable < S > entities);
Optional < T > findById(ID id);
boolean existsById(ID id);
Iterable < T > findAll();
Iterable < T > findAllById(Iterable < ID > ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll();
}
- long count() - Returns the number of entities available.
- void delete(T entity) - Deletes a given entity.
- void deleteAll() - Deletes all entities managed by the repository.
- void deleteAll(Iterable<? extends T> entities) - Deletes the given entities.
- void deleteById(ID id) - Deletes the entity with the given id.
- boolean existsById(ID id) - Returns whether an entity with the given id exists.
- Iterable findAll() - Returns all instances of the type.
- Iterable findAllById(Iterable ids) - Returns all instances of the type with the given IDs.
- Optional findById(ID id) - Retrieves an entity by its id.
- save(S entity) - Saves a given entity.
- Iterable saveAll(Iterable entities) - Saves all given entities.
The PagingAndSortingRepository<T, ID extends Serializable> interface
package org.springframework.data.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@NoRepositoryBean
public interface PagingAndSortingRepository < T, ID > extends CrudRepository < T, ID > {
/**
* Returns all entities sorted by the given options.
*
* @param sort
* @return all entities sorted by the given options
*/
Iterable < T > findAll(Sort sort);
/**
* Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object.
*
* @param pageable
* @return a page of entities
*/
Page < T > findAll(Pageable pageable);
}
The QueryDslPredicateExecutor interface
package org.springframework.data.querydsl;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
public interface QuerydslPredicateExecutor < T > {
Optional < T > findOne(Predicate predicate);
Iterable < T > findAll(Predicate predicate);
Iterable < T > findAll(Predicate predicate, Sort sort);
Iterable < T > findAll(Predicate predicate, OrderSpecifier << ? > ...orders);
Iterable < T > findAll(OrderSpecifier << ? > ...orders);
Page < T > findAll(Predicate predicate, Pageable pageable);
long count(Predicate predicate);
boolean exists(Predicate predicate);
}
Spring Data JPA Repository Interfaces
- JpaRepository<T, ID extends Serializable> interface
- JpaSpecificationExecutor interface
JpaRepository<T, ID extends Serializable> interface
package org.springframework.data.jpa.repository;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
@NoRepositoryBean
public interface JpaRepository < T, ID > extends PagingAndSortingRepository < T, ID > , QueryByExampleExecutor < T > {
List < T > findAll();
List < T > findAll(Sort sort);
List < T > findAllById(Iterable < ID > ids);
<S extends T > List < S > saveAll(Iterable < S > entities);
void flush();
<S extends T > S saveAndFlush(S entity);
void deleteInBatch(Iterable < T > entities);
void deleteAllInBatch();
T getOne(ID id);
@Override <
S extends T > List < S > findAll(Example < S > example);
@Override <
S extends T > List < S > findAll(Example < S > example, Sort sort);
}
JpaSpecificationExecutor interface
package org.springframework.data.jpa.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
public interface JpaSpecificationExecutor<T> {
Optional<T> findOne(@Nullable Specification<T> spec);
List<T> findAll(@Nullable Specification<T> spec);
Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
List<T> findAll(@Nullable Specification<T> spec, Sort sort);
long count(@Nullable Specification<T> spec);
}
excellent
ReplyDelete