Skip to content

@CreatedAt annotation not working with nested document field #3547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
boy-ahoy opened this issue Jan 26, 2021 · 10 comments
Closed

@CreatedAt annotation not working with nested document field #3547

boy-ahoy opened this issue Jan 26, 2021 · 10 comments
Labels
status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue

Comments

@boy-ahoy
Copy link

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" }
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 26, 2021
@christophstrobl
Copy link
Member

Did you follow the setup to enable auditing in Spring Data MongoDB?

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 26, 2021
@boy-ahoy
Copy link
Author

Thanks for sharing the link.
My understanding is I don't need to enable audit if I just want couple of auto generated timestamp field.
I am not using, for example, @createdby field.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 26, 2021
@christophstrobl
Copy link
Member

You do not need to provide AuditorAware but still need to enable auditing.
As per spring-projects/spring-data-commons#1712 auditing annotations on nested types are generally supported. However navigating the path meta.createdAt requires the property (meta in that case) to be non null.
If the issue persists a minimal example would help us triage the cause.

@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Feb 2, 2021
@boy-ahoy
Copy link
Author

boy-ahoy commented Feb 3, 2021

Yeah enabling auditing worked.

@boy-ahoy boy-ahoy closed this as completed Feb 3, 2021
@alabotski
Copy link

I also faced such problem... It is not possible to store given information in a nested object

Judging by the questions on stackoverflow, I'm not the only one with this problem

@michelLinkapital
Copy link

I also faced such problem... It is not possible to store given information in a nested object

Judging by the questions on stackoverflow, I'm not the only one with this problem

I am needing this today. Unable to find a solution. WIll go manual setting ir and I don't like it

@xisvaldo
Copy link

No solution for this using nested/embeddable classes?

@alabotski
Copy link

alabotski commented Mar 30, 2023

@xisvaldo
I created abstract class with system fields and extends from it
Find solution with nested object failed
PS Sorry for English
PS 2 To me, it's very strange that this ticket is closed

@xisvaldo
Copy link

I followed this link and could achieve using @Embeddable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-feedback We need additional information before we can continue
Projects
None yet
Development

No branches or pull requests

6 participants