1. @EnableJpaAuditing, @Configuration 어노테이션 붙은 configuration 클래스 생성
@EnableJpaAuditing
@Configuration
public class JpaConfig {
}
2. 날짜 칼럼 가지는 추상 baseEntity 클래스 생성
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
@CreatedDate
@Column(updatable = false, nullable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime modifiedAt;
}
3. 아래 예시 entity 처럼 생성한 추상 BaseEntity 상속받으면 적용 됨
@Getter
@Setter
@ToString
@Entity
@Table(name = "booking")
public class BookingEntity extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 기본 키 생성을 DB에 위임합니다. (AUTO_INCREMENT)
private Integer bookingSeq;
private Integer passSeq;
private String userId;
@Enumerated(EnumType.STRING)
private BookingStatus status;
private boolean usedPass;
private boolean attended;
private LocalDateTime startedAt;
private LocalDateTime endedAt;
private LocalDateTime cancelledAt;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", insertable = false, updatable = false)
private UserEntity userEntity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "passSeq", insertable = false, updatable = false)
private PassEntity passEntity;
// endedAt 기준, yyyy-MM-HH 00:00:00
public LocalDateTime getStatisticsAt() {
return this.endedAt.withHour(0).withMinute(0).withSecond(0).withNano(0);
}
}
댓글