JPA Auditing LastModifiedBy/LastModifiedDate 在事务方法中访问时为空
JPA Auditing LastModifiedBy/LastModifiedDate are null when being accessed in the transaction method
我的审核设置如下所示,它们工作正常。问题是当我想在更新之前在事务方法中访问它们时,更新 id/date 总是空的,我不确定为什么。
@CreatedBy
@Column(name = "CREATE_ID", updatable = false, nullable = false)
private String createId;
@LastModifiedBy
@Column(name = "UPDATE_ID", nullable = false)
private String updateId;
@CreatedDate
@Column(name = "CREATE_DATE", updatable = false, nullable = false)
private Date createDate;
@LastModifiedDate
@Column(name = "UPDATE_DATE", nullable = false)
private Date updateDate;
正在调用保存的 create/update 个方法。
注意:这两项工作正常,creating/updating 数据库中的记录具有正确的 create/update 审计值。 问题是我无法访问在更新方法中更新 id/date,我不确定 why/how 是否修复它。
@Override
@Transactional
public MyObj create(MyObj myObj) {
MyObj createdMyObj = myObjRepo.save(myObj);
System.out.println(createdMyObj.getCreateId()); // This works fine
return createdMyObj;
}
@Override
@Transactional
public MyObj update(MyObj myObj) {
MyObj updatedMyObj = myObjRepo.save(myObj);
System.out.println(updatedMyObj.getUpdateId()); // This is null
return updatedMyObj;
}
Spring Data JPA 的审计功能基于 JPA 生命周期事件,the event PreUpdate
used to set the last-modified columns 仅在 JPA 实现实际更新数据库时触发,这在许多情况下是在事务结束时.
参见 JPA specification 的第 3.5.3 节:
The PreUpdate and PostUpdate callbacks occur before and after the database update operations to entity data respectively. These database operations may occur at the time the entity state is updated or they may occur at the time state is flushed to the database (which may be at the end of the transaction).
因此,如果您希望设置这些值,则需要刷新持久性上下文。
我的审核设置如下所示,它们工作正常。问题是当我想在更新之前在事务方法中访问它们时,更新 id/date 总是空的,我不确定为什么。
@CreatedBy
@Column(name = "CREATE_ID", updatable = false, nullable = false)
private String createId;
@LastModifiedBy
@Column(name = "UPDATE_ID", nullable = false)
private String updateId;
@CreatedDate
@Column(name = "CREATE_DATE", updatable = false, nullable = false)
private Date createDate;
@LastModifiedDate
@Column(name = "UPDATE_DATE", nullable = false)
private Date updateDate;
正在调用保存的 create/update 个方法。
注意:这两项工作正常,creating/updating 数据库中的记录具有正确的 create/update 审计值。 问题是我无法访问在更新方法中更新 id/date,我不确定 why/how 是否修复它。
@Override
@Transactional
public MyObj create(MyObj myObj) {
MyObj createdMyObj = myObjRepo.save(myObj);
System.out.println(createdMyObj.getCreateId()); // This works fine
return createdMyObj;
}
@Override
@Transactional
public MyObj update(MyObj myObj) {
MyObj updatedMyObj = myObjRepo.save(myObj);
System.out.println(updatedMyObj.getUpdateId()); // This is null
return updatedMyObj;
}
Spring Data JPA 的审计功能基于 JPA 生命周期事件,the event PreUpdate
used to set the last-modified columns 仅在 JPA 实现实际更新数据库时触发,这在许多情况下是在事务结束时.
参见 JPA specification 的第 3.5.3 节:
The PreUpdate and PostUpdate callbacks occur before and after the database update operations to entity data respectively. These database operations may occur at the time the entity state is updated or they may occur at the time state is flushed to the database (which may be at the end of the transaction).
因此,如果您希望设置这些值,则需要刷新持久性上下文。