spring-boot - 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 表
当没有一列设置为空时,日期会更新,但我希望它即使使用空值也能工作
解决方案
推荐阅读
- python - scikit-learn train_test_split 是否保留关系?
- java - 基于选择结果的基本锁定
- .htaccess - htaccess 文件中 https 的重定向规则不起作用
- c - 将提交推送到 GitHub
- python - Python Pandas:删除A列第一个空单元格下方的所有数据行
- c# - 单击电子邮件中的链接时显示特定记录
- sql - 从带有标签 a 或 b 和 c 的表中选择行
- javascript - 如何在 json/csv 中转换 sql 转储文件
- alfresco - 来自户外内容的常见元数据字段如何工作?
- kubernetes - 在 Kubernetes 集群上使用 etcdctl 时出错:“证书由未知权限签名”