์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ๊ณ์ฐ๊ธฐ๋ง๋ค๊ธฐ
- ํ์ดํ๋ก๊ทธ๋๋ฐ
- FilterChain
- ํ๊ณ
- ๋ถํธ์บ ํ
- ๊นํ๋ธ
- ์คํ๋ง
- ์ธํ ๋ฆฌ์ ์ด
- spring data jpa
- ๊ฑฐ๋ญ์ ๊ณฑ
- Spring Security
- Spring Data JDBC
- CLI๋ช ๋ น์ด
- java
- ๋ฌธ์์ด๋ค์ง๊ธฐ
- testing
- ๋ฐฑ์ค์๊ณ ๋ฆฌ์ฆ
- ์ ๋ค๋ฆญ์ค
- ์๋ฐ
- ์๋ฃ๊ตฌ์กฐ
- HTML
- Publishing
- ๊ทธ๋ฆฌ๋
- ์ปฌ๋ ์ ํ๋ ์์ํฌ
- fibonacci
- CSS
- ๋ฐฑ์๋
- ์ฒซ๊ธ์๋๋ฌธ์
- ๋ฐ์ผ๋ฆฌ์ฝ๋ฉ
- ์๊ณ ๋ฆฌ์ฆ
- Today
- Total
๋์ ๋ชจ์
[Spring Data JPA] Auditing ๋ณธ๋ฌธ
๐ช Auditing?
Auditing์ ‘๊ฐ์’๋ผ๋ ๋ป์ ๊ฐ๊ณ ์๋๋ฐ, Spring Data์์๋ Auditing์ผ๋ก ๋๊ฐ Entity๋ฅผ ์์ฑํ๊ณ ๋ณ๊ฒฝํ๋์ง์ ๋ํ ์์ ์ ํฌ๋ช ํ๊ฒ ์ถ์ ํ ์ ์๊ฒ ํด์ฃผ๋ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๋ค. ์์ฑ๋ ๋ชจ๋ Entity์ ์์ฑ๋ ์๊ฐ๊ณผ ์์ ๋ ์๊ฐ์ ์ผ์ผ์ด ์ ์ด์ฃผ๋ ๊ฒ์ ๋ฒ๊ฑฐ๋กญ๊ณ ๊ท์ฐฎ๊ธฐ๋ ํ์ง๋ง, ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ์ ์ด ์๊ธด๋ค.
๋ชจ๋ ๊ฐ์ฒด์ ๋ค ํ์ํจ ⇒ ์ฝ๋๋ฅผ ์ผ์ผ์ด ๋ค ์์ฑํด์ค์ผ ๋จ ⇒ ์ค์๋ก ๋นผ๋จน์ / ์ฝ๋ ์ค๋ณต
๐ช Using Auditing
โจ add dependencies in build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}
๐ช Use Auditing with code
๐ฉ๐ป๐ป BaseTime.java
- ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์์ ํ ๋ ์ ์ฅ๋๋ ์๊ฐ์ ๊ธฐ๋กํด์ค BaseTime Entity๋ฅผ ์์ฑํ๋ค.
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTime {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime modifiedAt;
}
โจ @EntityListeners(AuditingEntityListener.class)
- ํด๋์ค๋ ๋ฒจ์ ์์นํ๋ฉฐ, ์์ฑ์๊ฐ, ์์ ์๊ฐ์ ์๋ํํ๊ฒ ๋ค๊ณ ์๋ ค์ค๋ค.
- AuditingEntityListner ์์ touchForCreate / touchForUpdate ์ด ๊ตฌํ๋์ด ์๋ค.
โจ @MappedSuperclass
- ๊ฐ์ฒด์๋ ์์์ด ์์ง๋ง ํ ์ด๋ธ์๋ ์์์ด ์๋๋ฐ ์ด๋ป๊ฒ ๋งคํํ ๊น? ์ด ๋ @MappedSuperclass๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
- ์์๋ฐ์ ์์ํด๋์ค์ ํ๋๋ณ์๋ค์ด ์ปฌ๋ผ๊ณผ ๋งคํ๋๋ค.
โจ @CreatedDate
- Entity๊ฐ ์์ฑ๋๊ณ ์ ์ฅ๋ ๋ ์๊ฐ์ด ์๋์ผ๋ก ์ ์ฅ๋๋ค.
โจ @Column(updatable = false)
- ์์ฑ ์๊ฐ์ ์์ฑํ ๋ ํ ๋ฒ๋ง ๊ธฐ๋ก๋๋ ์๊ฐ์ผ๋ก ์์ ๋๋ฉด ์ ๋๊ธฐ ๋๋ฌธ์ updatable์ false๋ก ํ๋ค. ์ค์๋ก ๊ฐ์ ๋ฐ๊ฟ๋ ์์ ๋ ๊ฐ์ด ์ ์ฅ๋์ง ์๋๋ค.
โจ @ LastModifiedDate
- Entity ๊ฐ์ ์์ ํ๊ณ ์ ์ฅํ ๋ ๋ณ๊ฒฝ๋ ์๊ฐ์ด ์๋์ผ๋ก ์ ์ฅ๋๋ค.
๐ฉ๐ป๐ป AuditingEntity.java
- Entity ํด๋์ค์ BaseTime๋ฅผ ์์ํ๋ค.
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import auditing.BaseTime;
import javax.persistence.*;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class AuditingEntity extends BaseTime {
@Id @GeneratedValue
@Column(name = "auditing_id")
private Long id;
private String name;
private String hobby;
}
๐ฉ๐ป๐ป SpringBootApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableJpaAuditing
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
โจ @EnableJpaAuditing
- ๋ง์ง๋ง์ผ๋ก Spring Data JPA์์ Auditing์ ์ฌ์ฉํ๋ ค๋ฉด SpringBoot์ ํด๋์ค ๋ ๋ฒจ์ @EnableJpaAuditing์ ์ ์ด์ ์๋ ค์ค์ผ ํ๋ค.
โ Ref.
https://docs.spring.io/spring-data/jpa/docs/1.7.0.DATAJPA-580-SNAPSHOT/reference/html/auditing.html
'WILT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Session] ์ ๋ฐฐ์ ์ฐธ๊ฒฌ์์ (0) | 2022.07.28 |
---|