몬그로이

Jpa repository 메서드 명명규칙 - 공식문서 참고 본문

Organizing Docs/Spring Docs

Jpa repository 메서드 명명규칙 - 공식문서 참고

Mon Groy 2024. 7. 12. 23:51
Parsing query method names is divided into subject and predicate. The first part (find…By, exists…By) defines the subject of the query, the second part forms the predicate. The introducing clause (subject) can contain further expressions. Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results.

 

find 와 By 사이 또는 exists 와 By 사이는 Top/First/Distinct 을 제외하면 설명문으로 인식한다

 

List<Person> findByAddressZipCode(ZipCode zipCode);
Assume a Person has an Address with a ZipCode. In that case, the method creates the x.address.zipCode property traversal. The resolution algorithm starts by interpreting the entire part (AddressZipCode) as the property and checks the domain class for a property with that name (uncapitalized). If the algorithm succeeds, it uses that property. If not, the algorithm splits up the source at the camel-case parts from the right side into a head and a tail and tries to find the corresponding property — in our example, AddressZip and Code. If the algorithm finds a property with that head, it takes the tail and continues building the tree down from there, splitting the tail up in the way just described. If the first split does not match, the algorithm moves the split point to the left (Address, ZipCode) and continues.

 

위와 같은 메서드의 경우 AddressZipCode 로 찾기 시작한다

그러다가 찾지 못할 경우 AddressZip 과 Code로 나눠서 찾는데,

이도 없을 경우는 Address 와 ZipCode 로 찾는다

 

 
Although this should work for most cases, it is possible for the algorithm to select the wrong property. Suppose the Person class has an addressZip property as well. The algorithm would match in the first split round already, choose the wrong property, and fail (as the type of addressZip probably has no code property).
To resolve this ambiguity you can use _ inside your method name to manually define traversal points. So our method name would be as follows: 
List<Person> findByAddress_ZipCode(ZipCode zipCode);

 

Person 클래스에 addressZip 이라는 멤버변수 또한 있다고 가정했을 때,

앞서 설명한 원리에 의해 찾는 방식은 결과에 모호성을 가지고 온다

그렇기 때문에 두 변수명 사이에 언더바(_)를 사용하여 구분해준다

 

 

'Organizing Docs > Spring Docs' 카테고리의 다른 글

MVC 디자인패턴  (0) 2024.07.18
동시성 제어 요약  (0) 2024.07.16
QueryDSL  (0) 2024.06.30
Entity-Table 에 사용하기 좋은 어노테이션 기능  (0) 2024.06.30
MVC 의 annotation 들  (0) 2024.05.25