我正在用jpa(springdatajpaversion2.3.0/hibernate5.1.0)运行一个spring启动项目,我如何强制使用@createdby注解@entity的字段的值?
实体:
import lombok.Getter;
import lombok.Setter;
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 javax.persistence.*;
import java.util.Date;
@Entity(...)
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
public class SomeEntity{
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_timestamp", nullable = false, updatable = false)
@CreatedDate
private Date createTimestamp;
@Column(name = "create_user")
@CreatedBy
private String createUser;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "update_timestamp")
@LastModifiedDate
private Date updateTimestamp;
@Column(name = "update_user")
@LastModifiedBy
private String updateUser;
}
配置:
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@Component
public class AuditConfig implements AuditorAware<String> {
/**
* The Constant SYSTEM_ACCOUNT.
*/
public static final String SYSTEM_ACCOUNT = "system";
/**
* Gets the current auditor.
*
* @return the current auditor
*/
@Override
public Optional<String> getCurrentAuditor() {
String currentUser = getUserFromSecurityContext();
return Optional.ofNullable(currentUser);
}
}
问题是,即使我使用以下代码强制值:
someEntity.setCreateUser("some other user");
该值被jpa自动覆盖。除了在表/实体中创建另一个字段之外,还有其他解决方案吗?
暂无答案!
目前还没有任何答案,快来回答吧!