DB변경을 누가했는지, 생성 주체, 생성 일자, 변경 주체, 변경 일자를 기록
@SpringBootApplication
@EnableJpaAuditing
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
@Configuration
@EnableJpaAuditing
public class JpaAuditingConfiguration {
}
@MappedSuperclass
: 상속받을 경우 자식 클래스에게 매핑 정보 전달@EntityListeners(AuditingEntityListener.class)
: Entity를 DB에 적용하기 전후에 콜백을 요청할 수 있도록@CreatedDate
: 생성 날짜를 자동으로 주입@LastModifiedDate
: 수정 날짜를 자동으로 주입@Getter
@Setter
@ToString
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
callSuper = true
: 부모 클래스의 필드를 포함extends BaseEntity
: BaseEntity 상속@Entity
...
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Visit extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
}