Skip to content

@CreatedAt annotation not working with nested document field #3547

Closed
@boy-ahoy

Description

@boy-ahoy

I am using Spring Boot 2.4.0.

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; // can be enum?
  }
}

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" }

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: feedback-reminderWe've sent a reminder that we need additional information before we can continuestatus: waiting-for-feedbackWe need additional information before we can continue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions