SpringData 구조 및 JpaRepository 원리

more·2023년 7월 27일
0

JPA 심화 2일차

목록 보기
1/5

SpringData 구조

SpringData 기능 목록

  • 강력한 리포지토리 및 사용자 지정 객체 매핑 추상화
  • 리포지토리 메서드 이름에서 동적 쿼리 파생
  • 기본 속성을 제공하는 구현 도메인 기본 클래스
  • 명료한 추적기능 지원(생성일시, 마지막 변경일시, 생성자, 마지막 변경자)
    -> Auditing 기능 (Timestamp class 만들 때에 사용했었음)
  • 사용자 지정 리포지토리 코드 통합 가능성
  • JavaConfig 및 사용자 지정 XML 네임스페이스를 통한 간편한 Spring 통합
  • Spring MVC 컨트롤러와의 고급 통합
  • 교차 스토어 지속성에 대한 실험적 지원

SpringData Jpa 와 JpaRepository 원리

  • Repository 는 MarkerInterface 로 특별한 기능은 없음
    -> 빈 껍데기처럼 존재하는 것
  • Repository ~ JpaRepository 까지는 @NotRepositoryBean이 붙어있는 인터페이스이다.
    -> Bean으로 등록을 하지 않겠다는 뜻
    - JpaRepository<Entity,ID> 붙이면 알맞은 프로그래밍 된 SimpleJpaReository 구현체 빈이 등록된다.
    -> 뒤에 <Entity, ID> 가 붙어있지 않으면 빈으로 등록이 되지 않는다.
    - @SpringBootApplication을 통해 자동으로 붙여지는 @EnableJpaRepositoriesJpaRepositoriesRegistrar 를 통해서 등록된다.
    - JpaRepositoriesRegistrar 는 ImportBeanDefinitionRegistrar 의 구현체이다
    - ImportBeanDefinitionRegistrar 는 프로그래밍을 통해 빈을 주입해준다.

기존 Repository vs 새로운 JpaRepository

  • 기존 Repository
    • @Repository 을 클래스에 붙인다.
      -> 해당 Annotation을 붙이면 Repository Component로 등록된다. (빈으로 등록)
    • 앞서배운 RawJPA의 Repository 기능만 가진 구현체가 생성된다. (DB별 예외처리 등)
  • 새로운 JpaRepository
    • JpaRepository<Entity,ID> 인터페이스를 인터페이스에 extends 붙인다.
      • @NotRepositoryBean상위 인터페이스들의 기능을 포함한 구현체가 프로그래밍된다. (@NotRepositoryBean = 빈생성 막음)
      • SpringDataJpa 에 의해 엔티티의 CRUD, 페이징, 정렬 기능 메소드들을 가진 빈이 등록된다. (상위 인터페이스들의 기능)

Repository 기능을 제한하는 방법

1. @RepositoryDefinition 을 인터페이스에 붙이는법 (가장 많이 쓰임)

  • 어노테이션을 붙이면 BeanDefinition 에 직접 접근하여 프로그래밍으로 주입받을 구현체 메소드들을 지정해서 요청할 수 있다.
  • 예시 코드
// Comment라는 클래스 형식을 가지고 아이디는 Long인 클래스
@RepositoryDefinition(domainClass = Comment.class, idClass = Long.class)
public interface CommentRepository {

    Comment save(Comment comment);

    List<Comment> findAll();
    
}

2. @NoRepositoryBean 인터페이스로 한번더 감싸는법

  • 상위 인터페이스 개념을 하나 더 만들어서 열어줄 메소드만 선언해준다.
  • 예시 코드
// 해당 어노테이션을 붙이면 빈으로 등록 안됨
@NoRepositoryBean
public interface MyRepository<T, ID extends Serializable> extends Repository<T, ID> {

    <E extends T> E save(E entity);

    List<T> findAll();

}
profile
조금 더

0개의 댓글