# 페이징

페이징에서 반드시 order by가 들어가야 제대로 페이징되는지 확인할 수 있다.\
왜냐하면 sorting이 되면서 순서대로 가져와야하기 때문이다.

JPA는 페이징을 다음 두 API로 추상화

1. setFirstResult(int startPosition) : 조회 시작 위치 (0부터 시작)
2. setMaxResults(int maxResult) : 조회할 데이터 수

따라서 개발자는 조회 시작할 위치와 조회할 데이터 수만 넣어주면 된다!

```java
//페이징 쿼리
String jpql = "select m from Member m order by m.name desc"; 
List<Member> resultList = em.createQuery(jpql, Member.class) 
    .setFirstResult(10) 
    .setMaxResults(20) 
    .getResultList(); 
```

엔티티 매핑정보와 Dialect 정보를 합쳐서 쿼리가 실행된다.\
H2의 Dialect 페이징 표준은 limit과 offset이다.

페이징 API - MySQL 방언

```sql
SELECT
    M.ID AS ID,
    M.AGE AS AGE,
    M.TEAM_ID AS TEAM_ID,
FROM
    MEMBER M
ORDER BY
    M.NAME DESC LIMIT ?, ?
```

페이징 API - Oracle 방언 : **3 Depth** ROWNUM

```sql
SELECT * FROM
    ( SELECT ROW_.*, ROWNUM ROWNUM_
    FROM
        ( SELECT
            M.ID AS ID,
            M.AGE AS AGE,
            M.TEAM_ID AS TEAM_ID,
            M.NAME AS NAME
        FROM MEMBER M
        ORDER BY M.NAME
        ) ROW_
    WHERE ROWNUM <= ?
    )
WHERE ROWNUM_ > ?
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/jpa-basic/10.-1/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
