I am using Spring Boot and Spring Data JPA and wanted to update the LastUpdatedDate
and LastModifiedBy
fields in JPA update statement. Assume Employee is my entity class which extends AbstractBaseEntity
class.
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Data
public abstract class AbstractBaseEntity {
@CreatedBy
@JsonIgnore
@Column(name = "CRTE_USER_ID")
protected String createdByUser = "TEST_USER";
@CreatedDate
@JsonIgnore
@Column(name = "CRTE_DT", updatable = false)
protected Date createdDate = new Date();
@LastModifiedBy
@JsonIgnore
@Column(name = "UPDT_USER_ID")
protected String lastUpdatedByUser = "TEST_USER";
@LastModifiedDate
@JsonIgnore
@Column(name = "UPDT_DT")
protected Date lastUpdatedOn = new Date();
@Version
@JsonIgnore
@Column(name = "VER_NUM")
protected Integer versionNumber = 1;
}
Another class
public class Employee extends AbstractBaseEntity{
...
...
...
}
Below query not updating any date. How can we fix it?
@Modifying(clearAutomatically = true)
@Transactional
@Query("UPDATE Employee p SET p.status =:status, p.lastUpdatedOn = :lastUpdatedOn WHERE p.srcProjectKeyId = (:id)")
int updatestatus(@Param("status") String status, @Param("id") Long id, @Param("lastUpdatedOn") Date lastUpdatedOn);