首页 > 解决方案 > SpringBoot MongoDB 继承 CreatedDate 和 LastModifiedDate 变为 null

问题描述

我有一个在所有模型中通用的字段的基类

@Getter
@Setter
public class BaseModel {
    @Id
    private String id;
    @CreatedDate
    private Date createdAt;
    @LastModifiedDate
    private Date modifiedAt;
    @Version
    private Long version;
}

和其他模型正在继承这个类,例如

@Document(collection = "accounts")
@Getter
@Setter
@ToString
public class Account extends BaseModel {
    @Indexed(unique = true)
    private String name;
}

当我持久化帐户对象时,我可以看到 createdAt 和 modifiedAt 被持久化,但是当我查询同一个帐户对象时,我得到 createdAt 和 modifiedAt 为空。我也启用了 mongo 审计。我也尝试过 TypeAlias 但没有工作,任何建议或帮助将不胜感激

标签: javaspringmongodbspring-boot

解决方案


推荐阅读