Why?
JPA Repository 를 만들때 자료를 찾아보면 사용되는 인터페이스가 JpaRepository, CRUDRepository 이 두개가 가장 많이 나와 어떤 차이점이 있는지 알아보았다.
아래 캡쳐된 프로젝트
Spring boot 3.0.4
What?
ChatGpt 에게 물어봤을 때 다음과 같이 답변해주었다.
JpaRepository
JpaRepository는 CRUDRepository를 상속하며, 다양한 데이터 액세스 기능을 제공하는 인터페이스입니다. JpaRepository는 단순한 CRUD 연산뿐만 아니라, 페이징, 정렬, Native 쿼리, 벌크 연산 등을 지원합니다. JpaRepository를 사용하면 더욱 복잡한 데이터 조작을 할 수 있으며, 다양한 데이터 액세스 기능을 제공하여 기능 확장성이 높습니다.
CRUDRepository
CRUDRepository는 데이터베이스에서 CRUD(Create, Read, Update, Delete) 작업을 수행하는 메소드를 제공하는 인터페이스입니다. 간단한 CRUD 작업이 필요한 경우에 사용할 수 있으며, 단일 엔티티 클래스를 조작하는 경우에 적합합니다.
프로젝트 내에서 다이어그램을 봤을 때 아래 사진과 같은 구조를 나타내주었다.
JpaRepository 이전 Paging과 Sorting은 'PagingAndSortingRepository' 인터페이스를 상속하기 때문에 가능하고 추가적인 기능은 JpaRepository에서 구현이 된다.
Other?
추가적으로 saveAll() 메소드 실행 시,
JpaRepository 는 List,
CrudRepository 에서는 Iterable 을 반환한다.
둘중 뭐가 좋다 이런것 보다 각 인터페이스의 특징을 보고 상황에 맞게끔 쓰면 좋을 것 같다.
참고
What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA? When I see the examples on the web, I see them there used kind of interchangeably. What is the diff...
stackoverflow.com
[JPA] JpaRepository 인터페이스와 CrudRepository
위 구조를 보면 Repository 인터페이스를 기준으로 CrudRepository, PagingAndSortingRepository, ...
blog.naver.com
'DEV > Spring' 카테고리의 다른 글
[Spring] Spring Security 란? (0) | 2023.07.26 |
---|---|
[Spring] Exception Handler (with. Spring Data Rest) (1) | 2023.07.12 |
[Spring] Spring Data Rest 란? (0) | 2023.07.10 |
[Spring] Spring Framework 란? (0) | 2023.03.31 |
[JPA] Unique 제약 조건 (1) | 2023.03.28 |