Reputation: 1949
I am using Spring Boot 2.4.0 along with Spring Data Mongo.
The POJO class mapped to collection is:
@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;
}
}
I am using MongoRepository to save above POJO in DB using save().
But I don't see the createdAt and updatedAt being populated in the collection.
This is what is being saved in collection:
{ "_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" }
Upvotes: 0
Views: 4817
Reputation: 5133
Have you enable the auditing? https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.auditing
Upvotes: 2