created_at과 updated_at 속성은 여러 엔티티에서 공통적으로 사용되므로, 이를 BaseTimeEntity로 분리하고, 해당 속성이 필요한 엔티티들은 BaseTimeEntity를 상속받아 사용하도록 구현하였다.
- BaseTimeEntity
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class) // 엔티티의 수정/생성등의 이벤트가 발생하였을 때, 이와 같은 변경사항을 Audit하기 위함
public class BaseTimeEntity {
@CreatedDate
@Column(name="created_at", updatable = false, columnDefinition = "timestamp")
private LocalDateTime createdAt;
@LastModifiedDate
@Column(name="updated_at", columnDefinition = "timestamp")
private LocalDateTime updatedAt;
}
- @MappedSuperclass : BaseEntity를 상속한 엔티티들이 BaseEntity의 필드들을 칼럼으로 인식하게 된다.
- @EntityListeners(AuditingEntityListener.class) : 특정 엔티티 클래스에 적용되어, 해당 엔티티에서 발생하는 엔티티의 수정/생성 등의 이벤트를 감지하고 처리, 기록하는데 사용
- @CreatedDate : Entity가 생성되어 저장될 때 날짜와 시간이 db에 자동으로 저장된다.
- @LastModifiedDate : Entity의 값을 변경할 때 날짜와 시간이 db에 자동으로 저장된다.
- BaseTimeEntity를 상속
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
public class Comment extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="comment_id")
private Long id;
@NotNull
@Column(columnDefinition = "text")
private String content;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="post_id")
private Post post;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id")
private User user;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="parent_id")
private Comment parent;
@OneToMany(mappedBy="parent")
private List<Comment> children = new ArrayList<>();
}
- 어플리케이션의 main method가 있는 클래스에 @EnableJpaAuditing 적용하기
@SpringBootApplication
@EnableJpaAuditing
public class InstagramApplication {
public static void main(String[] args) {
SpringApplication.run(InstagramApplication.class, args);
}
}
- @EnableJpaAuditing : 어플리케이션의 main method가 있는 클래스에 적용하며 JPA Auditing(감시) 기능을 어플리케이션 전역적으로 활성화하기 위한 어노테이션이다.
'백 > spring boot' 카테고리의 다른 글
S3에 이미지 업로드 & 삭제 (0) | 2024.10.10 |
---|---|
cascade type 속성 + N+1문제 해결법 결론 (0) | 2024.09.29 |
JPA 관련 문제들 (주로 N+1문제) (0) | 2024.09.29 |
스프링 빈 (0) | 2024.09.29 |
IoC/DI, AOP, PSA (0) | 2024.09.29 |