我的更新列(log_updated_at)遇到问题。当我创建实体时,将使用与创建日期列相同的日期来创建它。 但是,当更新某些字段时,日期不会更改。 我想更改日期。
在我的主班上,我有那些注释:
@Slf4j
@SpringBootApplication
@EnableTransactionManagement
@EnableJpaAuditing
我的auditModel:
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 tables in database not updated when entity field is modified
当没有任何一列设置为null时,日期会更新,但我希望即使使用null值也能使用