스프링 JPA @Query 어노테이션 질문
조회수 464회
package com.example.spbpractice.domain.posts;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface PostsRepository extends JpaRepository<Posts, Long> {
@Query("SELECT p FROM Posts p ORDER BY p.id DESC")
List<Posts> findAllDesc();
}
<스프링 부트와 AWS로 혼자 구현하는 웹 서비스>라는 책에 나오는 코드의 일부입니다. @Query 어노테이션을 이용해서 DB에 조회 쿼리를 날리고, 이를 이용해 게시판의 글 목록을 출력하는 부분인데요.
SELECT p
가 어떻게 동작하는 건지 잘 모르겠습니다.
보통 전체 조회를 할 때는 SELECT *
를 사용하는데, 그렇게 하니까 SQL 에러가 발생합니다.
@Query(nativeQuery = true, value = "SELECT '*' FROM Posts AS p ORDER BY p.id DESC;")
> org.hibernate.exception.SQLGrammarException: could not execute query
'SELECT * ...'가 먹히지 않는 이유가 무엇인가요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력