1

I encounter a problem on my Update colum (log_updated_at). When i create an entity, it is created with a date, same as the creation date column. But when some fields are updated, the date don't change. I would like the date to change when something change.

On my Main class i have those annotations :

@Slf4j
@SpringBootApplication
@EnableTransactionManagement
@EnableJpaAuditing

My 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();
}

And an update :

@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;
            }
...
...
}

After the save, the update date is not changed : "logCreatedAt": "2020-11-03T21:00:23.000+01:00", "logUpdatedAt": "2020-11-03T21:00:23.000+01:00",

Thanks for help

[EDIT] This helped me ! Spring boot tables in database not updated when entity field is modified

When no one column is set to null, the date is updated, but i would like it works even with null value

Kévin
  • 497
  • 10
  • 37

0 Answers0