Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JPA审计中强制@CreatedBy的值_Java_Spring_Spring Boot_Jpa_Auditing - Fatal编程技术网

Java 如何在JPA审计中强制@CreatedBy的值

Java 如何在JPA审计中强制@CreatedBy的值,java,spring,spring-boot,jpa,auditing,Java,Spring,Spring Boot,Jpa,Auditing,我正在用JPA运行一个spring引导项目(spring数据JPA版本2.3.0/hibernate 5.1.0) 如何强制使用@CreatedBy注释@Entity字段的值 实体: import lombok.Getter; import lombok.Setter; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; impo

我正在用JPA运行一个spring引导项目(spring数据JPA版本2.3.0/hibernate 5.1.0) 如何强制使用@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);
    }
}
该值会被JPA自动覆盖。
除了在表/实体中创建另一个字段外,还有其他解决方案吗?

定义自己的
CustomCreatedBy
注释。使用
@PrePersist
,您将获得具有自定义注释的所有字段,如果它们为空,则设置指示当前用户的值;如果没有,则保留现有值。

因此,您首先配置Spring为您自动填充字段,然后您尝试自己填充它?是的,我希望为每个插入自动填充字段,但当我显式设置值时,我不希望audit覆盖我的值。那么,
@CreatedBy
不可能做到这一点。您只需自己设置该值(您可以使用
SecurityContextHolder
获取当前经过身份验证的用户),我会尝试的谢谢。你有没有关于如何做到这一点的例子?
someEntity.setCreateUser("some other user");