> For the complete documentation index, see [llms.txt](https://dodeon.gitbook.io/study/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dodeon.gitbook.io/study/kimyounghan-orm-jpa/11-object-oriented-query-intermidiate/using-entity.md).

# Entity 직접 사용

![](/files/-MaNZPwiNJ7BDKR0qGHQ)

* JPQL에서 id 대신 Entity를 넣으면 SQL에서 자동으로 해당 Entity의 기본키를 사용한다.

## 기본키

```java
public class JpaMain {

    public static void main(String[] args) {
        String jpql = "select m from Member m where m = :member";

        List resultList = em.createQuery(jpql)
                .setParameter("member", member).getResultList();
    }
}
```

* Entity를 파라미터로 넘기는 방식

```java
public class JpaMain {

    public static void main(String[] args) {
        String jpql = "select m from Member m where m.id = :memberId";

        List resultList = em.createQuery(jpql)
                .setParameter("memberId", memberId).getResultList();
    }
}
```

* 식별자를 직접 전달하는 방식

```sql
select m.*
from Member m
where m.id = ?
```

* 둘 다 같은 SQL을 실행한다.

## 외래키

```java
public class JpaMain {

    public static void main(String[] args) {
        Team team = em.find(Team.class, 1L);
        // 엔티티를 파라미터로 바로 전달
        // 연관 관계 매핑 설정에서 적어둔 FK를 사용한다.
        String qlString = "select m from Member m where m.team = :team";

        List resultList = em.createQuery(qlString)
                .setParameter("team", team).getResultList();
    }
}
```

```java
public class JpaMain {

    public static void main(String[] args) {
        // 키를 파라미터로 전달
        String qlString = "select m from Member m where m.team.id = :teamId";

        List resultList = em.createQuery(qlString)
                .setParameter("teamId", teamId).getResultList();
    }
}
```

```sql
select m.*
from Member m
where m.team_id = ?
```

* 외래 키 역시 Entity를 사용할 수 있다.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://dodeon.gitbook.io/study/kimyounghan-orm-jpa/11-object-oriented-query-intermidiate/using-entity.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.
