-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- JPA μν°ν°μ μλͺ μ£ΌκΈ°μ μ ν λ°©λ²
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# JPA Entityμ μλͺ μ£ΌκΈ° | ||
|
||
## Entityλ? | ||
|
||
- λ°μ΄ν°λ₯Ό νννλ POJO | ||
- μν°ν° ν΄λμ€λ λ°μ΄ν°λ² μ΄μ€μ ν μ΄λΈκ³Ό 맀νλ¨ | ||
- μν°ν° μΈμ€ν΄μ€λ ν μ΄λΈμ νλμ rowμ 맀νλ¨ | ||
- μν°ν° λ΄ νλλ ν μ΄λΈμ νλμ columnκ³Ό 맀νλ¨ | ||
|
||
## Entityμ μν | ||
|
||
### λΉμμ (new/transient) | ||
|
||
- κ°μ²΄λ μμ§λ§, μμμ± μ»¨ν μ€νΈμ μ μ₯λ μ μ΄ μλ μν | ||
|
||
### μμ (managed) | ||
|
||
- μμμ± μ»¨ν μ€νΈμ μ μ₯λ μν | ||
- μμμ± μ»¨ν μ€νΈμ κ΄λ¦¬λ₯Ό λ°μ | ||
- 1μ°¨ μΊμ | ||
- Dirty Checking | ||
- Lazy Loading | ||
|
||
### μ€μμ (detached) | ||
|
||
- μμμ± μ»¨ν μ€νΈμ μ μ₯λμλ€κ° λΆλ¦¬λ μν | ||
- detachμ μν κ°λ³ λΆλ¦¬ λλ μμμ± μ»¨ν μ€νΈμ μμ ν μ΄κΈ°ν(clear) λλ μ’ λ£(close)μ μν΄ μμ μνμ μν°ν°κ° μ€μμ μνλ‘ μ νλκ² λ¨ | ||
|
||
### μμ (removed) | ||
|
||
- λ°μ΄ν°λ² μ΄μ€μμμ μμ κ° μμ²λ μν | ||
- μ»€λ° μν μ ν΄λΉ μν°ν°μ 맀νλλ rowκ° μ κ±°λ¨ | ||
|
||
## Entityμ μν μ ν | ||
|
||
### λΉμμ β μμ | ||
|
||
```java | ||
public void persist(Object entity); | ||
``` | ||
|
||
- persistλ λΉμμ μν μν°ν°μ μμνλ₯Ό μννλ©°, λΉμμ μν μν°ν°λ§ μ²λ¦¬ν μ μμ | ||
- μ€μμ/μμ μνμ μν°ν°κ° μΈμλ‘ μ£Όμ΄μ§λ©΄ `EntityExistsException`μ λμ§ | ||
|
||
### λΉμμ/μ€μμ β μμ | ||
|
||
```java | ||
public <T> T merge(T entity); | ||
``` | ||
|
||
- mergeλ μ£Όμ΄μ§ μν°ν°λ₯Ό μμμ± μ»¨ν μ€νΈμ λ±λ‘ν¨ | ||
- λΉμμμ΄λ , μ€μμμ΄λ , μ¬μ§μ΄ μμ μνλ λͺ¨λ μ²λ¦¬ κ°λ₯ | ||
|
||
### μμ β μ€μμ | ||
|
||
```java | ||
public void detach(Object entity); | ||
``` | ||
|
||
- μμμ± μ»¨ν μ€νΈλ‘λΆν° μν°ν°λ₯Ό μ κ±° | ||
|
||
### μμ β μμ | ||
|
||
```java | ||
public void remove(Object entity); | ||
``` | ||
|
||
- μμ μν°ν°λ₯Ό μμ (DBλ‘λΆν° μ κ±°) | ||
- μμ μν μν°ν°λ§ μ²λ¦¬ κ°λ₯ | ||
- μμμ± μ»¨ν μ€νΈμ μλ μν°ν°(μ€μμ/λΉμμ)κ° μ£Όμ΄μ§λ©΄ `IllegalArgumentException`μ λμ§ | ||
|
||
## Spring Data JPAμ save | ||
|
||
```java | ||
@Transactional | ||
@Override | ||
public <S extends T> S save(S entity) { | ||
|
||
Assert.notNull(entity, "Entity must not be null"); | ||
|
||
if (entityInformation.isNew(entity)) { | ||
em.persist(entity); | ||
return entity; | ||
} else { | ||
return em.merge(entity); | ||
} | ||
} | ||
``` | ||
|
||
- isNew(entity)κ° μ°Έμ΄λ©΄ λΉμμ μνλΌλ λ»μ΄λ―λ‘ persistλ‘ μμν | ||
- κ·Έλ μ§ μλ€λ©΄ μ€μμ λλ μμ μνμΌ κ²μ΄λ―λ‘ mergeλ‘ μμν |