英文:
Exclude some fields from audit with Spring Data JPA @LastModifiedDate
问题
我有一个名为User的实体。这个实体包含了几个字段,其中一个是lastModifiedDate
:
@LastModifiedDate
@Column(name = "last_modified_date", columnDefinition = "DATETIME")
private ZonedDateTime lastModifiedDate;
每次表格被更新时,这个字段也会被更新。从本质上讲,这是可以的。
问题是,在同一个实体中,我还有另一个名为loginTime
的字段:
@Column(name = "login_time", columnDefinition = "DATETIME")
private ZonedDateTime loginTime;
每当新用户登录应用程序时,这个字段会被更新。
然而,当用户登录时,由于loginTime
字段被更新,字段lastModifiedDate
也会随之更新。
是否有办法阻止特定字段(比如loginTime
)更新时导致lastModifiedDate
被更新?
谢谢
英文:
I have an entity called User. This entity contains several fields and one of them is lastModifiedDate
:
@LastModifiedDate
@Column(name = "last_modified_date", columnDefinition = "DATETIME")
private ZonedDateTime lastModifiedDate;
Every time the table is updated, this field gets updated too. Which per se is fine.
The issue is that in the same entity I have also another field called loginTime
:
@Column(name = "login_time", columnDefinition = "DATETIME")
private ZonedDateTime loginTime;
This field is updated whenever a new user logs into the application.
However, when users log in, since the loginTime
field is updated, the field lastModifiedDate
is also updated consequently.
Is there a way to prevent lastModifiedDate
from being updated when specific fields (like loginTime
) are updated?
Thank you
答案1
得分: 1
你可以使用带有 @Query
注解的 JPQL 更新查询来更新仅 loginTime
字段,这样 lastModifiedDate
字段将不会被更新。
@Modifying
@Query("update User u set u.loginTime = :loginTime where u.id = :id")
int updateLoginTime(@Param("loginTime") ZonedDateTime loginTime, @Param("id") Integer id);
英文:
You can use JPQL update query using @Query
to update only loginTime
field then lastModifiedDate
field will not be updated.
@Modifying
@Query("update User u set u.loginTime = :loginTime where u.id = :id")
int updateLoginTime(@Param("loginTime") ZonedDateTime loginTime, @Param("id") Integer id);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论