来自父 class 的日期字段的更新在 Spring Data Jpa 中不起作用
Update of Date fields from Parent class not working in Spring Data Jpa
我正在使用 Spring Boot 和 Spring Data JPA,想更新 JPA 更新语句中的 LastUpdatedDate
和 LastModifiedBy
字段。假设 Employee 是我的实体 class 扩展 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;
}
另一个class
public class Employee extends AbstractBaseEntity{
...
...
...
}
以下查询未更新任何日期。我们该如何解决?
@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);
为了make auditing work at all,请确保@EnableJpaAuditing
注解添加到@Configuration
class.
AuditingEntityListener
方法在 @PrePersist
和 @PreUpdate
阶段被调用。 AFAIK 只有在您直接操作实体时才有效。如果您使用 @Query
语句,它将不起作用。
更新语句必须手动执行,例如:
UPDATE Employee p SET p.status =:status, p.lastUpdatedOn =
:lastUpdatedOn, p.lastUpdatedBy = :lastUpdatedBy WHERE
p.srcProjectKeyId = (:id)
要获取当前日期,请使用:
Date now = new Date();
// or
long now = System.currentTimeMillis();
获取当前用户(a.k.a.principal)使用:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
这里是关于缓存失效问题的更长时间的讨论:
Spring Data JPA Update @Query not updating?
我正在使用 Spring Boot 和 Spring Data JPA,想更新 JPA 更新语句中的 LastUpdatedDate
和 LastModifiedBy
字段。假设 Employee 是我的实体 class 扩展 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;
}
另一个class
public class Employee extends AbstractBaseEntity{
...
...
...
}
以下查询未更新任何日期。我们该如何解决?
@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);
为了make auditing work at all,请确保@EnableJpaAuditing
注解添加到@Configuration
class.
AuditingEntityListener
方法在 @PrePersist
和 @PreUpdate
阶段被调用。 AFAIK 只有在您直接操作实体时才有效。如果您使用 @Query
语句,它将不起作用。
更新语句必须手动执行,例如:
UPDATE Employee p SET p.status =:status, p.lastUpdatedOn =
:lastUpdatedOn, p.lastUpdatedBy = :lastUpdatedBy WHERE
p.srcProjectKeyId = (:id)
要获取当前日期,请使用:
Date now = new Date();
// or
long now = System.currentTimeMillis();
获取当前用户(a.k.a.principal)使用:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
这里是关于缓存失效问题的更长时间的讨论:
Spring Data JPA Update @Query not updating?