Skip to content

Commit

Permalink
Add jpa/Entity Manager
Browse files Browse the repository at this point in the history
- JPA의 μ—”ν‹°ν‹° λ§€λ‹ˆμ €μ™€ μ˜μ†μ„± μ»¨ν…μŠ€νŠΈ
  • Loading branch information
hyh1016 committed May 9, 2024
1 parent ceb80eb commit e183e82
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions java/jpa/Entity_Manager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Entity Manager

## Entity Manager

- JPA κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” 객체
- 내뢀에 DataSource(Database Connection)λ₯Ό μ†Œμœ 
- μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 레벨의 가상 λ°μ΄ν„°λ² μ΄μŠ€λ‘œ λ³Ό 수 있음
- Thread-safeν•˜μ§€ **μ•ŠμŒ**. μ—¬λŸ¬ μŠ€λ ˆλ“œκ°€ ν•˜λ‚˜μ˜ Entity Manager 객체λ₯Ό κ³΅μœ ν•˜λ©΄ μ•ˆ 됨
- EntityManager와 달리 EntityManagerFactoryλŠ” 생성 λΉ„μš©μ΄ 큰 객체둜 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ „μ²΄μ—μ„œ 단 ν•œ 번만 μƒμ„±ν•˜κ³  κ³΅μœ ν•΄μ„œ μ‚¬μš©ν•΄μ•Ό 함 (Singleton)

## Persistence Context (μ˜μ†μ„± μ»¨ν…μŠ€νŠΈ)

- μ—”ν‹°ν‹° λ§€λ‹ˆμ €κ°€ λ‚΄λΆ€μ μœΌλ‘œ κ΄€λ¦¬ν•˜λŠ” μ—”ν‹°ν‹° μƒνƒœ λ³΄κ΄€μ†Œ
- μ—”ν‹°ν‹° λ˜ν•œ 객체이고, κΈ°λŠ₯이 μˆ˜ν–‰λ˜λ©° λ‚΄λΆ€ 값이 변경됨
- μ˜μ†μ„± μ»¨ν…μŠ€νŠΈλŠ” μ—”ν‹°ν‹°μ˜ 졜초 등둝 μ‹œμ μ˜ μƒνƒœλ₯Ό **μŠ€λƒ…μƒ·**으둜 μ €μž₯ν•˜κ³ , λ°μ΄ν„°λ² μ΄μŠ€μ— λ°˜μ˜ν•˜λŠ” μ‹œμ κ³Ό μŠ€λƒ…μƒ·μ„ 비ꡐ해 쿼리λ₯Ό 싀행함

### μ—”ν‹°ν‹° 생λͺ…μ£ΌκΈ°

- μ˜μ†μ„± μ»¨ν…μŠ€νŠΈλ₯Ό κΈ°μ€€μœΌλ‘œ μ—”ν‹°ν‹°λŠ” 4가지 μƒνƒœλ₯Ό κ°€μ§ˆ 수 있음
1. λΉ„μ˜μ†(new/transient) - 아직 μ˜μ†μ„± μ»¨ν…μŠ€νŠΈμ— μ €μž₯된 적 μ—†μŒ
2. μ˜μ†(managed) - μ˜μ†μ„± μ»¨ν…μŠ€νŠΈμ— μ €μž₯λ˜μ–΄ 있음
3. μ€€μ˜μ†(detached) - μ˜μ†μ„± μ»¨ν…μŠ€νŠΈμ— μ €μž₯λ˜μ—ˆλ‹€κ°€ 뢄리됨
4. μ‚­μ œ(removed) - μ—”ν‹°ν‹°κ°€ μ‚­μ œλ¨
- μ‹λ³„μžκ°€ μ—†λ‹€λ©΄ λΉ„μ˜μ† μƒνƒœλ‘œ λ³Ό 수 있고, μ‹λ³„μžκ°€ μžˆλ‹€λ©΄ μ˜μ†μ„± μ»¨ν…μŠ€νŠΈμ˜ 관리λ₯Ό λ°›λŠ”μ§€ 여뢀에 따라 μ˜μ†κ³Ό μ€€μ˜μ†μœΌλ‘œ λ‚˜λ‰¨. μ‚­μ œ μƒνƒœλŠ” DB에 반영될 λ•Œ delete 쿼리가 μ‹€ν–‰λ˜λŠ” μƒνƒœλ₯Ό 말함

### μ˜μ†μ„± μ»¨ν…μŠ€νŠΈκ°€ μ§€μ›ν•˜λŠ” κΈ°λŠ₯

1. **1μ°¨ μΊμ‹œ**
- μ˜μ†μ„± μ»¨ν…μŠ€νŠΈλŠ” μΊμ‹œμ˜ 역할을 함. νŠΉμ • μ—”ν‹°ν‹°λ₯Ό μ‘°νšŒν•˜λ €κ³  ν•  λ•Œ 1μ°¨ μΊμ‹œμ— ν•΄λ‹Ή μ—”ν‹°ν‹°κ°€ μžˆλ‹€λ©΄ DBλ₯Ό μ‘°νšŒν•˜μ§€ μ•Šκ³  ν•΄λ‹Ή μ—”ν‹°ν‹°λ₯Ό λ°˜ν™˜ν•¨
- μΊμ‹œ ν‚€λŠ” μ‹λ³„μž, μΊμ‹œ 값은 μ—”ν‹°ν‹° κ·Έ 자체
2. **동일성 보μž₯**
- μ˜μ†μ„± μ»¨ν…μŠ€νŠΈμ—μ„œλŠ” 같은 μ‹λ³„μžλ₯Ό 가진 μ—”ν‹°ν‹°λ₯Ό μ‘°νšŒν•˜λ©΄ 항상 같은 객체λ₯Ό λ°˜ν™˜ν•¨
3. **μ“°κΈ° 지연**
- νŠΈλžœμž­μ…˜μ˜ μ›μžμ„±μ— κΈ°λ°˜ν•œ κΈ°λŠ₯으둜, λͺ¨λ“  μ“°κΈ°(insert/update/delete) 쿼리듀을 νŠΈλžœμž­μ…˜μ˜ 컀밋 μ‹œμ μ— μˆ˜ν–‰λ˜λ„λ‘ 함
- μ˜μ†μ„± μ»¨ν…μŠ€νŠΈμ— λ‹΄κΈ°λŠ” 변경듀이 νŠΈλžœμž­μ…˜ λ²”μœ„ μ•ˆμ—μ„œ 이루어지기 λ•Œλ¬Έμ—, μ „λΆ€ λ°˜μ˜ν•˜κ±°λ‚˜ λ‘€λ°±ν•˜κ±°λ‚˜ λ‘˜ 쀑 ν•˜λ‚˜μ΄κΈ° λ•Œλ¬Έμ— νš¨μœ¨μ„±μ„ μœ„ν•΄ 컀밋 μ‹œμ μ— ν•œ λ²ˆμ— λ°˜μ˜ν•˜λŠ” 것
- μœ μ˜ν•  점 - commit == μ“°κΈ° 지연 μˆ˜ν–‰μ΄ μ•„λ‹˜. μ •ν™•νžˆλŠ” 컀밋 이전에 μˆ˜ν–‰ν•˜λŠ” flushκ°€ μ“°κΈ° 지연 쿼리 μˆ˜ν–‰μ„ μœ λ°œν•˜λŠ” λ™μž‘μž„. 컀밋 μ‹œμ μ΄ μ•„λ‹ˆλ”λΌλ„ flushλ₯Ό ν˜ΈμΆœν•΄ μ§€μ—°λœ μ“°κΈ° 쿼리듀을 μ‹€ν–‰ν•  수 있음
- μœ μ˜ν•  점 - IDENTITY μ‹λ³„μž μ „λž΅μ„ μ‚¬μš©ν•˜λŠ” μ—”ν‹°ν‹°μ˜ insert μ‹œμ μ—λŠ” μ“°κΈ° 지연 없이 λ°”λ‘œ 쿼리가 λ°œμƒν•¨. κΈ°λ³Έ ν‚€ 값을 νšλ“ν•˜κΈ° μœ„ν•¨
4. **λ³€κ²½ 감지**
- μ“°κΈ° 쿼리가 μˆ˜ν–‰λ  μ‹œμ μ— μ—”ν‹°ν‹°μ˜ μŠ€λƒ…μƒ·κ³Ό ν˜„μž¬ μƒνƒœλ₯Ό 비ꡐ해 변경을 λ°˜μ˜ν•˜κΈ° μœ„ν•œ 쿼리듀을 μˆ˜ν–‰ν•¨
- λ³€κ²½ 감지 λ˜ν•œ flush μ‹œμ μ— μˆ˜ν–‰
5. **지연 λ‘œλ”©**
- μ—°κ΄€ 관계인 μ—”ν‹°ν‹°λ₯Ό 일단 ν”„λ‘μ‹œλ‘œ κ°€μ Έμ™”λ‹€κ°€ μ‚¬μš©ν•˜λŠ” μ‹œμ μ— 쑰회 쿼리λ₯Ό 톡해 λ‘œλ”©ν•΄μ˜€λŠ” μ „λž΅

0 comments on commit e183e82

Please sign in to comment.