Method name strategy

최종윤·2023년 1월 6일

JPA

목록 보기
10/15

https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-creating-database-queries-with-the-query-annotation/

method 이름으로 db queries를 생성하는 법을 배워보자

2가지 조건을 만족하는 search function을 구현해보겠습니다.
1. 검색 단어(term)를 포함하는 title or description을 갖는 todo entries를 return
2. 대소문자 구별x

Query Methods 생성

다음 규칙에 따라 Query Methods를 생성한다.

규칙

  • query method 이름은 다음과 같이 시작해야 합니다.
    prefixes: find…By, read…By, query…By, count…By, and get…By.
  • By 앞에 the First or the Top keyword 추가해 query results의 수를 1개로 제한할 수 있다.
  • the First and the Top keywords 에 숫자를 붙여 query results를 여러 개 가져올 수 있다.
    For example, findTopBy, findTop1By, findFirstBy, and findFirst1By
    모두 조건에 맞는 1개의 entity를 반환한다.
  • Distinct keyword 를 By 앞에 두어 중복 없는 query results를 반환하게할 수 있다..
    For example, findTitleDistinctBy or findDistinctTitleBy
    은 unique 한 Title entites를 db에서 조회하여 반환한다.
  • property expressions with the supported keywords를 결합하여 검색 조건을
    Query Methods에 추가한다.
  • query method의 검색 조건을 x로 하면 method parameter에 x가 있어야 하고 ,
    method parameter의 수가 search condition의 수와 같다.
    method parameter의 순서와 search condition의 순서가 같다.

예제

Example 1: If we want to create a query method that returns the todo entry whose id is given as a method parameter, we have to add one of the following query methods to our repository interface:

(o)public Todo findById(Long id);

(o!!)public Optional<Todo,> findById(Long id);

Example 2: If we want to create a query method that returns todo entries whose title or description is given as a method parameter, we have to add the following query method to our repository interface:

(X)public List<Todo,> findByTitleOrDescription(String term);

(O)public List<Todo,> findByTitleOrDescription(String title,String description);

Example 3: If we want to create a query method that returns the number of todo entries whose title is given as a method parameter, we have to add the following query method to our repository interface:

public long countByTitle(String title);

Example 4: If we want to return the distinct todo entries whose title is given as a method parameter, we have to add the following query method to our repository interface:

public List<Todo,> findDistinctByTitle(String title);

Example 5: If we want to to return the first 3 todo entries whose title is given as a method parameter, we have to add one of the following query methods to our repository interface:

(X)public List<Todo.> findFirst3ByTitle(String title);

public List<Todo.> findFirst3ByTitleOrderByTitleAsc(String title);

public List<Todo.> findTop3ByTitleOrderByTitleAsc(String title);

Search Function 구현

term을 포함하는 description 조건을 query로 구현하기 위해
property expression 으로 Description
keyword는 Contains 를 method name에 더한다.

조건1이 true이거나 조건2가 true일때 반환하게 하려면 Or을 method name에 더한다.

Title을 포함하는 query를 생성하고 앞의 조건과 Or로 연결한다.

case 구별을 하지 않고 검색을 하기 위해 AllIgnoreCase를 method name 뒤에 붙인다.

  • List<Todo.> findByDescriptionContainsOrTitleContainsAllIgnoreCase(String descriptionPart, String titlePart);

언제 Method name strategy?

장점

간단한 쿼리는 빨리 만들 수 있고, method 이름으로 검색 condition과 value를 알 수 있다. This query generation strategy has the following weaknesses:

단점

  • method name parser가 query를 생성한다. parser가 요구되는 keyword를 지원하지 않으면 method name으로 query를 생성하지 못한다.
  • 조건이 3개 이상이면 method name이 너무 길어진다.
  • 동적 쿼리를 지원하지 않는다.

가독성 좋게 하기위해 1개 혹은 2개일때 사용한다.

profile
https://github.com/jyzayu

0개의 댓글