TIL

Spring Boot 웹개발 Entity 상속관계 만들기

everyday-spring 2024. 9. 2. 20:43
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
public abstract class BaseTimeEntity {

    @CreatedDate
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime updatedAt;
}

JPA에서 제공하는 create, update time을 사용한 BaseTimeEntity class

 

import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;

@Getter
@MappedSuperclass
public abstract class BaseAuditEntity extends BaseTimeEntity {

    @CreatedBy
    private Long createdBy;

    @LastModifiedBy
    private Long updatedBy;
}

 

import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;

@Getter
@MappedSuperclass
public abstract class BaseEntity extends BaseAuditEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

 

Entity에서 BaseEntity를 상속받아 사용한다

'TIL' 카테고리의 다른 글

Code Convention 도구 "Spotless"  (0) 2024.09.05
팀과제 피드백  (0) 2024.09.04
Spring Data 페이징, 정렬 기능  (0) 2024.08.27
Entity 연관 관계 ( 1 : 1 / N : 1 / 1 : N / N : M )  (0) 2024.08.23
SpringBoot mvc Repository  (0) 2024.08.14