首页 > 解决方案 > Spring Boot @LastModifiedDate 未更新

问题描述

我在更新列 (log_updated_at) 上遇到问题。当我创建一个实体时,它是用一个日期创建的,与创建日期列相同。但是当某些字段被更新时,日期不会改变。我希望在某些事情发生变化时更改日期。

在我的主课上,我有这些注释:

@Slf4j
@SpringBootApplication
@EnableTransactionManagement
@EnableJpaAuditing

我的审计模型:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@MappedSuperclass
@Data
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
        value = {"logcreatedAt", "logupdatedAt"},
        allowGetters = true
)
public abstract class AuditModel implements Serializable {
    
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @Column(name = "log_created_at", nullable = false, updatable = false)
    @CreatedDate
    private Date logCreatedAt = new Date();
    
    @Temporal(TemporalType.TIMESTAMP) 
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @Column(name = "log_updated_at", nullable = false)
    @LastModifiedDate
    private Date logUpdatedAt = new Date();
}

和更新:

@Component
@Slf4j
public class SaveCheckConfig {

    @Autowired
    private CheckConfigRepository checkConfigRepository;

    @Autowired
    private CheckNamespaceAndService checkNamespaceAndService;

    @Autowired
    private SaveCheckConfig saveCheckConfig;

    @Autowired
    private MappingCheckConfigToDTO mappingCheckConfigToDTO;

    @Transactional(rollbackFor=Exception.class)
    public CheckConfig saveCheckConfigMethod(CheckConfigDTO checkConfigDTO) throws InternalServerErrorException {
        try {       
            Namespace namespace = checkNamespaceAndService.getNamespace(checkConfigDTO.getNamespace());
            Service service = checkNamespaceAndService.getServices(checkConfigDTO.getService());
            CheckConfig checkConfigexist = checkConfigRepository.findByNamespaceIdAndServiceIdAndTypeVerification(namespace.getId(), service.getId(), checkConfigDTO.getType_verification());
            if (checkConfigexist != null) { 
                checkConfigexist.setDetails(checkConfigDTO.getDetails());
                checkConfigexist.setStatus(checkConfigDTO.getStatus());
                checkConfigexist.setStatusComplementaire(checkConfigDTO.getStatus_complementaire());
                checkConfigexist.setActionBy(checkConfigDTO.getAction_by());
                checkConfigRepository.save(checkConfigexist);   
                log.info("[CheckConfig] Update réalisé pour -> Namespace : " + checkConfigDTO.getNamespace() + " -> Service : " + checkConfigDTO.getService() + " -> TypeVerification : " + checkConfigDTO.getType_verification()); 
                return checkConfigexist;
            }
...
...
}

保存后,更新日期不变:“logCreatedAt”:“2020-11-03T21:00:23.000+01:00”,“logUpdatedAt”:“2020-11-03T21:00:23.000+01:00” ,

感谢帮助

[编辑] 这对我有帮助! 修改实体字段时未更新数据库中的 Spring Boot 表

当没有一列设置为空时,日期会更新,但我希望它即使使用空值也能工作

标签: spring-bootdatespring-data-jpaspring-annotations

解决方案


推荐阅读