DATA JPA

최종윤·2022년 12월 30일

JPA

목록 보기
5/15

출처 : https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-introduction/

JPA 와 data JPA

JPA쓰는 Repository는 많은 시간이 들고, 많은 반복되는 코드가 있다.
똑같이 반복되는 코드를 다음과 같이 제거할 수 있다.
1. Entity에 대해 CRUD를 제공하는 추상 repository를 생성한다.
2. 추상 repo를 상속시키는 concrete repo를 생성한다.
db query들을 생성하고 호출하는 일을 , db query를 생성할 때마다 반복해야한다.

-> DATA JPA를 쓰면 db query를 생성하고 호출하는 코드를 반복 작성하지 않아도 됨.

SPRING DATA JPA 는 3계층이 있다.

SPRING DATA JPA는 interface를 상속시켜서 jpa repo를 생성을 지원한다.
SPRING DATA COMMONS 는 infastructure을 제공한다. datastore specific Spring Data projects에서 공유되는 infastructure이다.
JPA PROVIDER 는 jpa를 구현한다.

이렇게 추가 계층을 추가해서 복잡해졌지만 반복되는 코드 작성을 없애준다.
다음은 Spring Data Commons 프로젝트가 제공하는 인터페이스다.

The Repository<T, ID extends Serializable> interface is a marker interface that has two purposes:
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.

The CrudRepository<T, ID extends Serializable> interface provides CRUD operations for the managed entity.

The PagingAndSortingRepository<T, ID extends Serializable> interface declares the methods that are used to sort and paginate entities that are retrieved from the database.

The QueryDslPredicateExecutor<> interface is not a "repository interface". It declares the methods that are used to retrieve entities from the database by using QueryDsl Predicate objects.

두번째로, Spring Data JPA 프로젝트가 제공하는 인터페이스다.

The JpaRepository<T, ID extends Serializable> interface is a JPA specific repository interface that combines the methods declared by the common repository interfaces behind a single interface.

The JpaSpecificationExecutor<> interface is not a "repository interface". It declares the methods that are used to retrieve entities from the database by using Specification<> objects that use the JPA criteria API.

다음은 interface들의 계층구조이다.

다음 interface들의 사용방법은 다음 단계를 따른다.
1. repo interface를 생성하여 Spring Data에서 제공하는 interface를 상속시킨다.
2. custom query method들을 repo interface에 추가한다.
3. repo interface를 다른 Component(Service) 에 주입시켜 Spring에 의해 자동으로 제공되는 구현을 사용한다.

요약

Spring Data JPA는 Spring provider가 아니다. JPA(JPA Provider)를 repo 추상화에 숨긴다.
Spring Data 는 다양한 목적들로 사용되는 여러개 repo interface들을 제공한다.

profile
https://github.com/jyzayu

0개의 댓글