Saurav Prakash
Saurav Prakash

Reputation: 1949

Spring Data Mongo @CreatedAt annotation not working with nested document field

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

Answers (1)

Related Questions