프로젝트를 하다보니 select * from comment where article_id=1 order by desc; 이런 Query를 사용해야 하는 경우가 있었다. 이럴 때 @Query 어노테이션으로 JPQL을 쓰면 되겠다 싶긴 했는데, 뭔가

Spring Data JPA  Repository 에서 이정도는 메서드 이름만으로 지원해주지 않을까 싶었다.

 

그리고 정말 지원해주고 있었다!

public interface CommentRepository extends JpaRepository<Comment, Long> {

    List<Comment> findAllByArticleIdOrderByCreateDateDesc(Long articleId);
}

 


메소드 이름으로 JPQL 을 생성해주는 기능 규칙 공식문서

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details

한글로 잘 정리된 블로그

https://joont92.github.io/jpa/Spring-Data-JPA/

+ Recent posts