Spring JPA 는 @ CreatedDate, @ CreatedBy, @ LastModified Date, @ LastModified By 를 사용 하여 자동 으로 시간 과 수정 자 를 생 성 합 니 다.
spring jpa 에 서 는 필드 나 방법 에 대한 주해
@CreatedDate
, @CreatedBy
, @LastModifiedDate
, @LastModifiedBy
를 지원 합 니 다.@CreatedDate
이 필드 는 시간 필드 를 만 드 는 것 으로 이 실체 가 insert 에 있 을 때 값 @CreatedBy
이 필드 는 창설 자 를 나타 내 며, 이 실체 가 insert 에 있 을 때 값 을 설정 합 니 다 @LastModifiedDate
, @LastModifiedBy
같은 이치 입 니 다.어떻게 사용 합 니까?
먼저 실체 류 를 밝 히 려 면 클래스 에 주해
@EntityListeners(AuditingEntityListener.class)
를 더 해 야 하고, 애플 리 케 이 션 시작 클래스 에 주해 EnableJpaAuditing
를 더 해 야 하 며, 필요 한 필드 에 @CreatedDate
, @CreatedBy
, @LastModifiedDate
, @LastModifiedBy
등 주 해 를 더 해 야 한다.이 때 jpa. save 방법 이 호출 될 때 시간 필드 는 자동 으로 데이터 베 이 스 를 설정 하고 삽입 합 니 다. 그러나 Created By 와 LastModified By 는 값 을 부여 하지 않 았 습 니 다.
AuditorAware
인 터 페 이 스 를 실현 해 야 하기 때문에 삽입 할 값 을 되 돌려 줍 니 다.import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class WalletApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(WalletApplication.class).web(true).run(args);
}
}
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
@Configuration
public class UserIDAuditorBean implements AuditorAware {
@Override
public Long getCurrentAuditor() {
SecurityContext ctx = SecurityContextHolder.getContext();
if (ctx == null) {
return null;
}
if (ctx.getAuthentication() == null) {
return null;
}
if (ctx.getAuthentication().getPrincipal() == null) {
return null;
}
Object principal = ctx.getAuthentication().getPrincipal();
if (principal.getClass().isAssignableFrom(Long.class)) {
return (Long) principal;
} else {
return null;
}
}
}
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
/**
* .
* @author Wang.ch
*
*/
@Entity
@Table(name = "store_source_bind")
@EntityListeners(AuditingEntityListener.class)
public class StoreSourceBind {
/**
*
*/
@Column(name = "create_time")
@CreatedDate
private Date createTime;
/**
*
*/
@Column(name = "create_by")
@CreatedBy
private Long createBy;
/**
*
*/
@Column(name = "lastmodified_time")
@LastModifiedDate
private Date lastmodifiedTime;
/**
*
*/
@Column(name = "lastmodified_by")
@LastModifiedBy
private String lastmodifiedBy;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.