更新时间:2023-11-30 23:34:04
您的代码按预期工作.实现 Persistable
后,您可以看到 @CreatedDate
注释正在工作.
Your code is working as expected. After you've implemented Persistable
you can see that @CreatedDate
annotation is working.
确保 createdDate
在第二次调用 save
时为 null
,因为该对象已存在于数据库中并且您使用 createdDate = null
.正如您从 @CreatedDate
的文档中看到的:
Sure that createdDate
is null
on the second call of save
because the object already exists in the database and you updated it with createdDate = null
. As you can see from the documentation for @CreatedDate
:
@CreatedDate 注释.这标识了其值被设置的字段当实体第一次持久化到数据库时.
@CreatedDate annotation. This identifies the field whose value is set when the entity is persisted to the database for the first time.
所以不要在第二次调用时用 null
覆盖您的 createdDate
,您应该使用 c = repository.findOne("test_id" 从数据库中检索您的客户);
然后更新它.
So not to overwrite your createdDate
with null
on the second call you should retrieve your customer from the database with c = repository.findOne("test_id");
and then update it.