데이터의 생성시간, 수정시간, 생성한 사람, 마지막으로 수정한 사람을 저장해야 할 때가 있습니다.
생성 시간 수정 시간 저장을 자동화하고 BaseEntity로 만들어서, 필요한 엔티티들은 모두 BaseEntity를 상속받는다.
package com.example.loans_domain.auth.entity;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {
@CreatedDate
private LocalDateTime createdAt;
// @CreatedBy
// @Column(updatable = false)
// private String createdBy;
@LastModifiedDate
private LocalDateTime modifiedAt;
// @LastModifiedBy
// private String lastModifiedBy;
}
- @MappedSuperclass:
- 이 어노테이션을 사용하면, 이 클래스를 상속받는 엔티티 클래스들이 BaseTimeEntity의 필드를 자신의 필드처럼 사용할 수 있게 됩니다. 즉, 이 클래스에 정의된 필드들은 상속받는 엔티티 클래스의 테이블에 컬럼으로 추가됩니다.
- @EntityListeners(AuditingEntityListener.class):
- 이 어노테이션은 엔티티의 변화(생성, 수정 등)를 감지하고 특정 행동을 할 수 있도록 합니다. AuditingEntityListener는 JPA의 Auditing 기능을 활용하여 엔티티가 생성되거나 수정될 때 자동으로 특정 필드를 업데이트하는 역할을 합니다.
- @CreatedDate:
- 엔티티가 처음 생성될 때 생성 시간을 자동으로 기록합니다. 이 필드는 엔티티가 저장되는 시점에 자동으로 설정됩니다.
- 필드명: createdAt
- 타입: LocalDateTime
- @LastModifiedDate:
- 엔티티가 수정될 때 수정 시간을 자동으로 기록합니다. 이 필드는 엔티티가 수정될 때마다 자동으로 업데이트됩니다.
- 필드명: modifiedAt
- 타입: LocalDateTime
- @CreatedBy:
- 엔티티가 처음 생성될 때 누가 생성했는지를 기록하는 필드입니다.
- @Column(updatable = false): 이 어노테이션은 이 필드가 한 번 설정된 후에는 업데이트되지 않음을 나타냅니다.
- 필드명: createdBy
- 타입: String
- @LastModifiedBy:
- 엔티티가 수정될 때 누가 수정했는지를 기록하는 필드입니다.
- 필드명: lastModifiedBy
- 타입: String
package com.example.loans_domain.auth.entity;
import com.example.loans_domain.auth.entity.constant.Gender;
import com.example.loans_domain.auth.entity.constant.Role;
import jakarta.persistence.*;
import lombok.*;
import java.time.LocalDateTime;
@Entity
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class UserEntity extends BaseTimeEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long user_id; // 사용자 고유 ID
@Column(nullable = false, unique = true)
private String user_email; // 사용자 이메일
@Column(nullable = false, unique = true, length = 20)
private String user_username; // 사용자 이름
private String user_password; // 사용자 비밀번호
private String user_nickname; // 사용자 닉네임
private String user_mobile; // 사용자 휴대전화 번호
private Gender user_gender; // 사용자 성별 (MALE, FEMALE, OTHER)
private LocalDateTime user_birth; // 사용자 생년월일
private String user_address; // 사용자 주소
private Role user_role; // 사용자 역할 (USER, ADMIN, MODERATOR)
private LocalDateTime create_at; // 계정 생성 시간
private LocalDateTime update_at; // 계정 정보 마지막 수정 시간
}
package com.example.loans_domain;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@SpringBootApplication
@EnableJpaAuditing
public class LoansDomainApplication {
public static void main(String[] args) {
SpringApplication.run(LoansDomainApplication.class, args);
}
}