> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/jpa-basic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/jpa-basic/3./persistence-context1.md).

# Persistence Context1

JPA에서 가장 중요한 2가지

1. 객체와 관계형 데이터베이스 매핑하기(Object Relational Mapping) : 정적
2. 영속성 컨텍스트 : 엔티티를 영구 저장하는 환경\
   실제 동작하는 매커니즘은 영속성 컨텍스트를 통해 이루어진다!<br>

![](/files/-MeUuJ0oOFLExNabOGfO)

EMF를 통해 고객 요청 올 때마다 EM 생성한다.\
EM은 내부적인 커넥션(커넥션 풀)을 사용해서 DB를 사용한다.

**em.persist(entity) : 엔티티를 DB에 저장한다는 뜻이 아니라 영속성 컨텍스트에 저장한다!**\
그러므로 em.persist하기 전(before) 후(after)로 출력문을 남겨서 디버깅해보면 예상되는 결과는 before -> SQL -> after 이지만 실제 결과는 before -> after -> SQL 이다!\
엔티티가 영속 상태가 되는 시점이 아니라 트랜잭션이 커밋되는 시점에 SQL 쿼리가 DB에 들어가고 업데이트된다!

엔티티의 생명주기

1. 비영속(new/transient) : 영속 컨텍스트에 없고, JPA와 관계 없는 상태(JPA가 처리하지 않는다.)
2. 영속(managed) : EM 안에 영속 컨텍스트에 엔티티를 넣는 개념! 영속 컨텍스트를 통해 JPA가 엔티티를 관리!\
   em.persist(member);
3. 준영속(detached) : 영속성 컨텍스트에 저장되었다가 분리된 상태\
   em.detach(member);
4. 삭제(removed) : 삭제된 상태\
   em.remove(member);


---

# 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, and the optional `goal` query parameter:

```
GET https://heunnajo.gitbook.io/jpa-basic/3./persistence-context1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
