【发布时间】:2021-01-26 08:01:11
【问题描述】:
我正在使用 Spring Boot 2.4.0 和 Spring Data Mongo。
映射到集合的 POJO 类是:
@SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"},
justification = "This is a bean")
@Document(collection = "Posts")
@Data
@Builder
public class Post {
@Id
private String id;
private ActorInfo actorInfo;
private String text;
private Media media;
private int likes;
private Meta meta;
@Version
private Integer version;
@Data
@Builder
public static class Meta {
@CreatedDate // <-- this is not working
private Instant createdAt;
@LastModifiedDate // <-- this is not working
private Instant updatedAt;
private String event;
}
}
我正在使用 MongoRepository 使用 save() 将上述 POJO 保存在 DB 中。
但我没有看到集合中填充了 createdAt 和 updatedAt。
这是保存在集合中的内容:
{ "_id" : ObjectId("600fc6b23d5ebf145b7bbc0d"), "actorInfo" : { "_id" : "1", "role" : "USER" }, "text" : "Helo", "likes" : 0, "meta" : { "event" : "CREATED" }, "version" : 0, "_class" : "com.highstreet.socialmediaservice.adpater.output.type.Post" }
【问题讨论】:
-
AFAIK 仅适用于根级别文档,
@Document不适用于嵌套属性/集合。 -
@M.Deinum 你有什么参考吗?
标签: java spring mongodb spring-data-mongodb