# Detached Status

준영속 상태

1. 영속 상태의 엔티티가 영속성 컨텍스트에서 분리(detached)
2. 영속성 컨텍스트가 제공하는 기능을 사용 못함

영속 상태가 되는 2가지 방법\
1차 캐시에 있는 상태를 영속 상태라고 할 수 있다.

1. em.persist(entity)
2. em.find(Member.class,id) : 데이터를 조회할 때 1차 캐시에 없는 경우 DB에서 조회해서 1차 캐시에 저장한다!

준영속 상태로 만드는 방법

1. em.detach(entity)  : 특정 엔티티만 준영속 상태로 전환
2. em.clear() : 영속성 컨텍스트를 완전히 초기화\
   em.clear() 이전과 이후에 동일한 엔티티를 조회하면 1차 캐시가 두 삭제 되기 때문에 쿼리가 한번 더 나간다!\
   테스트케이스 작성 시에 사용하기도 한다.
3. em.close()  : 영속성 컨텍스트를 종료


---

# 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/3./detached-status.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.
