Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 스프링 부트
- Application Event
- @ConfigurationProperties
- JsonSerializer
- webjar
- OAuth2
- 알고리즘
- Application Argument
- application.properties
- cors
- @Profile
- AuthenticationPrincipal
- HttpMessageConverters
- HATEOAS
- rest api
- 브루트포스
- WebApplication Type
- 리소스 서버
- 다익스트라
- 리소스핸들러
- 외부설정
- 백준
- EnableAutoConfiguration
- 정적 리소스
- 백기선
- 스프링부트
- JPA
- 백트래킹
- Application Runner
- Spring Security
Archives
- Today
- Total
아카이브
[스프링 데이터 JPA] 스프링 데이터 JPA 11. Auditing 본문
엔티티의 변경 시점에 언제, 누가 변경했는지에 대한 정보를 기록하는 기능
아쉽지만 이 기능은 스프링 부트가 자동 설정해주지 않습니다.
메인 애플리케이션 위에 @EnableJpaAuditing 추가(@EnableJpaAuditing에 AuditorAware 빈 이름 설정하기)
@SpringBootApplication
@EnableJpaAuditing(auditorAwareRef = "accountAuditAware")
@EnableJpaRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND
, repositoryImplementationPostfix = "Impl"
, repositoryBaseClass = SimpleYourRepository.class)
@Import(JumenRegister.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
엔티티 클래스 위에 @EntityListeners(AuditingEntityListener.class) 추가
@NamedEntityGraph(name = "Comment.post", attributeNodes = @NamedAttributeNode("post"))
@Entity
@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
public class Comment {
@Id
@GeneratedValue
private Long id;
private String comment;
/*
* ManyToOne에서 fetch default = EAGER
* */
@ManyToOne(fetch = FetchType.LAZY)
private Post post;
@CreatedDate
private Date created;
@CreatedBy
@ManyToOne
private Account createdBy;
@LastModifiedDate
private Date updated;
@LastModifiedBy
@ManyToOne
private Account updatedBy;
private Integer likeCount = 0;
private int up;
private int down;
private boolean best;
}
AuditorAware 구현체 만들기
@Service
public class AccountAuditAware implements AuditorAware<Account> {
@Override
public Optional<Account> getCurrentAuditor() {
System.out.println("looking for current user");
return Optional.empty();
}
}
JPA의 라이프 사이클 이벤트
Hibernate ORM 5.4.27.Final User Guide
Fetching, essentially, is the process of grabbing data from the database and making it available to the application. Tuning how an application does fetching is one of the biggest factors in determining how an application will perform. Fetching too much dat
docs.jboss.org
- Hibernate가 제공하는 기능
- 엔티티 변화에 따라 특정한 콜백 이벤트를 발생시켜준다.
- 엔티티에 콜백을 정의할 수 있다.
- 이를 통한 Audit 기능 구현이 가능하다.
Comment Entity에 라이프 사이클 이벤트 추가하기
public class Comment {
... 생략 ...
@PrePersist
public void prePersist() {
System.out.println("PrePersist is called");
this.created = new Date();
}
@PreUpdate
public void preUpdate() {
System.out.println("PreUpdate is called");
this.updated = new Date();
}
}
'Spring > 스프링 데이터 JPA' 카테고리의 다른 글
[스프링 데이터 JPA] 스프링 데이터 JPA 10. 트랜잭션 (0) | 2021.01.17 |
---|---|
[스프링 데이터 JPA] 스프링 데이터 JPA 9. Specifications (0) | 2021.01.17 |
[스프링 데이터 JPA] 스프링 데이터 JPA 8. Projection (0) | 2021.01.17 |
[스프링 데이터 JPA] 스프링 데이터 JPA 7. EntityGraph (0) | 2021.01.16 |
[스프링 데이터 JPA] 스프링 데이터 JPA 6. Update 쿼리 (0) | 2021.01.16 |