문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 마지막 판 양쪽 다음 판 | ||
java:jpa:springdatajpa [2018/09/21 09:05] kwon37xi |
java:jpa:springdatajpa [2023/07/10 15:06] kwon37xi [Pageable 사용 금지] |
||
---|---|---|---|
줄 5: | 줄 5: | ||
* [[java: | * [[java: | ||
- | ====== 기본 Repository Interface | + | ===== 기본 Repository Interface ===== |
< | < | ||
public interface MyEntityRepository extends JpaRepository< | public interface MyEntityRepository extends JpaRepository< | ||
줄 35: | 줄 35: | ||
</ | </ | ||
+ | ===== Repository 상속 금지 ===== | ||
+ | * 멀티 모듈 프로젝트에서 '' | ||
+ | * 이 경우 도메인 모듈과 말단 모듈의 Repository가 둘다 Custom 구현을 필요로 할 경우 제대로 작동안하게 된다. | ||
+ | * 또한, 메소드 이름 겹칠 때 뭘 선택하게 되는지 불확실한 등 문제가 많다. | ||
+ | * 꼭 말단 모듈에서 Repository 메소드를 도메인모듈과 격리해서 구현하고자 한다면 **상속 없이 별개의 Repository로 따로 만든다.** | ||
+ | |||
+ | ===== Pageable 사용 금지 ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * Spring Data 에서 '' | ||
+ | * 이 기능은 데이터량이 적은 tutorial 프로젝트 수준에서나 쓸모 있다. | ||
+ | * 대부분의 실무 서비스는 이런 방식의 페이징을 사용하면 시스템이 사용할 수 없을 정도로 큰 부하를 일으킨다. | ||
+ | * 다른 문제 : page의 시작인 기본이 0인데 문제는 보편적인 서비스의 page 번호는 1로 시작한다. 이를 헷갈려 해서 호출자가 첫번째 페이지를 1로 호출하는 경우가 많다. 설정으로 해결 가능하나 이를 지키지 못하는 경우가 많이 발생한다. | ||
+ | * [[database: | ||
+ | |||
+ | ===== Pageable 대신 Slice 사용 ===== | ||
+ | * [[https:// | ||
+ | * 페이징이 필요하다면 되도록 전체 갯수는 한 번만 세고 '' | ||
+ | * 다만, '' | ||
===== Native Query ===== | ===== Native Query ===== | ||
<code java> | <code java> | ||
줄 44: | 줄 64: | ||
* 컬럼이 하나 혹은 엔티티 객체일 경우에는 리턴값을 해당 타입 혹은 해당 타입의 리스트로 지정한다. | * 컬럼이 하나 혹은 엔티티 객체일 경우에는 리턴값을 해당 타입 혹은 해당 타입의 리스트로 지정한다. | ||
* 다중 컬럼일 경우에는 '' | * 다중 컬럼일 경우에는 '' | ||
+ | |||
+ | ===== Projection ===== | ||
+ | * 기본적으로 '' | ||
+ | * [[https:// | ||
===== Domain Support ===== | ===== Domain Support ===== | ||
줄 59: | 줄 83: | ||
* '' | * '' | ||
+ | ==== IntelliJ Spring Data Jpa Repository 자동생성 ==== | ||
+ | * **File and Code Templates**에서 다음 두가지를 '' | ||
+ | * **Reformat according to style**을 지정한다. | ||
+ | |||
+ | === Spring Data JPA Repository === | ||
+ | <code java> | ||
+ | #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "" | ||
+ | |||
+ | import org.springframework.stereotype.Repository; | ||
+ | import org.springframework.data.jpa.repository.JpaRepository; | ||
+ | import org.springframework.data.querydsl.QueryDslPredicateExecutor; | ||
+ | # | ||
+ | @Repository | ||
+ | public interface ${EntityClass}Repository extends JpaRepository< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Spring Data JPA Repository Custom Impl === | ||
+ | <code java> | ||
+ | #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "" | ||
+ | |||
+ | import org.springframework.data.jpa.repository.support.QueryDslRepositorySupport; | ||
+ | |||
+ | import javax.persistence.EntityManager; | ||
+ | import javax.persistence.PersistenceContext; | ||
+ | |||
+ | public class ${EntityClass}RepositoryImpl extends QuerydslRepositorySupport implements ${EntityClass}RepositoryCustom { | ||
+ | public ${EntityClass}RepositoryImpl() { | ||
+ | super(${EntityClass}.class); | ||
+ | } | ||
+ | |||
+ | @PersistenceContext | ||
+ | @Override | ||
+ | public void setEntityManager(EntityManager entityManager) { | ||
+ | super.setEntityManager(entityManager); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Java 8 과 함께 ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | <code java> | ||
+ | interface CustomerRepository extends Repository< | ||
+ | |||
+ | @Async | ||
+ | CompletableFuture< | ||
+ | | ||
+ | @Query(" | ||
+ | Stream< | ||
+ | | ||
+ | // CRUD method using Optional | ||
+ | Optional< | ||
+ | |||
+ | // Query method using Optional | ||
+ | Optional< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Exists ===== | ||
+ | * [[https:// | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ===== Query 인자로 들어온 null 무시하기 ===== | ||
+ | * [[https:// | ||
+ | * ''? | ||
+ | <code sql> | ||
+ | (? is null or column = ?) | ||
+ | </ | ||
+ | |||
+ | ===== Projection ===== | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * interface 기반으로 projection 가능. | ||
+ | |||
+ | ===== @Query에 SpEL 지원 ===== | ||
+ | * [[https:// | ||
+ | |||
+ | <code java> | ||
+ | @Query(" | ||
+ | List< | ||
+ | |||
+ | @Query(" | ||
+ | List< | ||
+ | </ | ||
===== 참조 ===== | ===== 참조 ===== | ||
줄 64: | 줄 176: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * https:// | ||
+ | * [[https:// | ||
+ | * [[https:// |