通过 "getById & save" 更新的 JPA 实体未更新其更新的时间戳列
JPA entity update via "getById & save" didn't updated it's updatedtimestamp column
我现在正在尝试使用“getById 并保存”来更新实体的列,如下所示。
// update "title" column
val entityRef = entityRepository.getById(entityID)
entityRef.title = futureTitle
return entityRepository.save(entityRef)
但是,如果我 运行 代码,更新的时间戳列(描述为 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)即使已完全更新,也不会更改。
似乎 updatedtimestamp
列仅在我使用自定义“UPDATE”查询时更新。
@Modifying
@Transactional
@Query("update Entity p set p.title = :newTitle where p.id = :entityId")
fun updateEntityTitle(entityId: Long, newTitle: String)
我能知道为什么通过“getById & save”更新实体没有对 updatedtimestamp
进行更改吗?
提前致谢!
ON UPDATE CURRENT_TIMESTAMP
是 MySql 的 SQL 方言的构造。描述如下in the documentation:
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).
由于所有 JPA 实现都会更新所有列,无论它们是否更改,它们也会为您的 CURRENT_TIMESTAMP
列提供一个值,从而阻止任何自动更新,如文档中所述。
您可以通过多种方式使用此技术堆栈获取更新时间戳:
您可以使用动态插入仅更新已更改的字段。请注意,在大多数情况下,这对性能不利。参见 Hibernate: Dirty Checking and Only Update of Dirty Attributes?
您可以使用 @Column(updatable=false)
将列设置为只读。参见
您可以使用 Hibernates UpdateTimestamp
for this purpose
而不是使用 MySql 功能
或者你用JPAs EntityListener
.
并且由于您似乎使用 Spring Data JPA,您也可以使用 Spring Datas @LastModifiedDate
.
我现在正在尝试使用“getById 并保存”来更新实体的列,如下所示。
// update "title" column
val entityRef = entityRepository.getById(entityID)
entityRef.title = futureTitle
return entityRepository.save(entityRef)
但是,如果我 运行 代码,更新的时间戳列(描述为 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)即使已完全更新,也不会更改。
似乎 updatedtimestamp
列仅在我使用自定义“UPDATE”查询时更新。
@Modifying
@Transactional
@Query("update Entity p set p.title = :newTitle where p.id = :entityId")
fun updateEntityTitle(entityId: Long, newTitle: String)
我能知道为什么通过“getById & save”更新实体没有对 updatedtimestamp
进行更改吗?
提前致谢!
ON UPDATE CURRENT_TIMESTAMP
是 MySql 的 SQL 方言的构造。描述如下in the documentation:
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).
由于所有 JPA 实现都会更新所有列,无论它们是否更改,它们也会为您的 CURRENT_TIMESTAMP
列提供一个值,从而阻止任何自动更新,如文档中所述。
您可以通过多种方式使用此技术堆栈获取更新时间戳:
您可以使用动态插入仅更新已更改的字段。请注意,在大多数情况下,这对性能不利。参见 Hibernate: Dirty Checking and Only Update of Dirty Attributes?
您可以使用
@Column(updatable=false)
将列设置为只读。参见您可以使用 Hibernates
而不是使用 MySql 功能UpdateTimestamp
for this purpose或者你用JPAs
EntityListener
.并且由于您似乎使用 Spring Data JPA,您也可以使用 Spring Datas
@LastModifiedDate
.