-
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 Entityμ μλͺ μ£ΌκΈ° λ³νμ λ°λΌ λ°νλλ μ΄λ²€νΈλ€
- Loading branch information
Showing
1 changed file
with
92 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,92 @@ | ||
# JPA Entity Lifecycle Event | ||
|
||
## JPA Entity Lifecycle | ||
|
||
- JPAμ μν°ν°μλ μλͺ μ£ΌκΈ°μ λ°λΌ λ°νλλ μ΄λ²€νΈλ€μ΄ μ‘΄μ¬ | ||
- μ΄λ¬ν μ΄λ²€νΈλ€μ μ΄λ Έν μ΄μ ννλ‘ μ§μλ¨ | ||
- `@PrePersist` - μμ μνλ‘ μ νλκΈ° μ μ νΈμΆ | ||
- `@PostPersist` - μμ μνλ‘ μ νλ ν νΈμΆ | ||
- `@PreLoad` - μμμ± μ»¨ν μ€νΈμ λ‘λλ ν νΈμΆ | ||
- `@PreUpdate` - μμ μ΄ λ°μνκΈ° μ μ νΈμΆ | ||
- `@PostUpdate` - μμ μ΄ λ°μν ν νΈμΆ | ||
- `@PreRemove` - μν°ν°κ° μ κ±°λκΈ° μ μ νΈμΆ | ||
- `@PostRemove` - μν°ν°κ° μ κ±°λ ν νΈμΆ | ||
|
||
## μ¬μ© λ°©λ² | ||
|
||
### Entityμ λ©μλ μ μΈ | ||
|
||
```java | ||
@Entity | ||
public class MyEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private long id; | ||
|
||
private String name; | ||
|
||
@PrePersist | ||
public void prePersist() { | ||
// μν°ν° μμν μ΄μ μ μνλ λ‘μ§ | ||
} | ||
|
||
@PostPersist | ||
public void postPersist() { | ||
// μν°ν° μμν μ΄ν μνλ λ‘μ§ | ||
} | ||
|
||
} | ||
``` | ||
|
||
- μ΄λ Έν μ΄μ μ΄ μ§μ λ μ½λ°± λ©μλλ₯Ό λ±λ‘νλ©΄ μν°ν° μνμ λ³νκ° μΌμ΄λ λ μλ§μ μ½λ°± λ‘μ§μ΄ μνλ¨ | ||
- λ°νκ°μ voidμ¬μΌ ν¨ | ||
- μμ κ°μ΄ μ¬μ©νλ κ²½μ°, μν°ν° λ΄μ μ μΈλ λ©μλμ΄λ―λ‘ thisλ₯Ό ν΅ν΄ λ΄λΆ νλμ μ κ·Ό κ°λ₯ | ||
|
||
### λ³λ ν΄λμ€μ μ μΈ ν EventListenersλ‘ λ±λ‘ | ||
|
||
```java | ||
public class MyJpaEventListener { | ||
|
||
@PrePersist | ||
public void prePersist(MyEntity myEntity) { | ||
// μν°ν° μμν μ΄μ μ μνλ λ‘μ§ | ||
} | ||
|
||
@PostPersist | ||
public void postPersist(MyEntity myEntity) { | ||
// μν°ν° μμν μ΄ν μνλ λ‘μ§ | ||
} | ||
|
||
} | ||
``` | ||
|
||
```java | ||
@Entity | ||
@EntityListeners(MyJpaEventListener.class) | ||
public class MyEntity { | ||
|
||
@Id | ||
@GeneratedValue | ||
private long id; | ||
|
||
private String name; | ||
|
||
} | ||
``` | ||
|
||
- μ¬λ¬ μν°ν°μμ 곡ν΅μ μΌλ‘ λ±λ‘νλ μ½λ°± λ©μλμ κ²½μ° μμ κ°μ΄ λ³λ 컀μ€ν 리μ€λλ₯Ό λ§λ€μ΄ μ¬μ¬μ©μ΄ κ°λ₯ | ||
- μΈμλ‘ μν°ν°λ₯Ό μ£Όμ λ°μ λ΄λΆ νλ κ°μ μ κ·Ό κ°λ₯ | ||
- μ£Όμν μ μ, μ΄ κ²½μ° spring boot application ν΄λμ€ λλ jpa configuration ν΄λμ€μ `@EnableJpaAuditing` μ΄λ Έν μ΄μ μ μ§μ ν΄μ€μΌ ν¨ | ||
|
||
## μ μ μ¬ν | ||
|
||
### μ½λ°± λ©μλμμ μμΈ λ°μ μ | ||
|
||
- μ΄λ²€νΈλ₯Ό λ°μ μ²λ¦¬νλ μ½λ°± λ©μλμμ μμΈκ° λ°μνλ©΄ **νΈλμμ μ λ‘€λ°±λ¨** | ||
- μλνλ©΄, λμΌ μ€λ λ λμΌ νΈλμμ μ€μ½ν λ΄μμ νΈμΆλλ λ‘μ§μ΄κΈ° λλ¬Έ | ||
- μ¦, μ΄λ²€νΈμ νΈμΆ μμ μ Pre~μ κ²½μ° νΈλμμ μ€μ½νλ₯Ό μ° λ€μ΄λ©°, Post~μ κ²½μ° λ«κΈ° μ μ | ||
|
||
## Reference | ||
|
||
[JPA Entity Lifecycle Events | Baeldung](https://www.baeldung.com/jpa-entity-lifecycle-events) |