조컴퓨터

BaseTimeEntity abstract class 본문

공부/JPA

BaseTimeEntity abstract class

챠오위 2023. 8. 18. 22:41

엔티티를 생성하면 공통적으로 들어가는 속성이 있는데, 이는 `생성일자` 와 `수정일자` 이다.

매번 작성하기 보다는 중복으로 발생하는 내용을 상속 받는 구조로 처리할 것이다.

 

 

BaseTimeEntity.java

@Getter
@MappedSuperclass
public abstract class BaseTimeEntity {

    @CreationTimestamp
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
}

 

User.java

@Getter
@Entity
public class Users extends BaseTimeEntity {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "user_id")
    private Long userId;

    @Column(unique = true, nullable = false)
    private String name;

    @Column(nullable = false)
    private String password;

    @Builder
    public Users(Long userId, String name, String password) {
        this.userId = userId;
        this.name = name;
        this.password = password;
    }

    protected Users() {
    }
}

 

프로젝트 진행하면서 배웠는데 참 편리한 기능이라 생각되어 글로 남긴다.

 

 

 

'공부 > JPA' 카테고리의 다른 글

영속성 전이: CASCADE, 고아 객체  (0) 2022.02.10
지연 로딩과 즉시 로딩  (0) 2022.02.10
양방향 매핑 시점  (0) 2022.02.08
JPQL 쿼리란  (0) 2022.02.06
[DB] 엔티티(Entity)란 무엇인가  (0) 2022.02.04