Sagar Gangwal
Sagar Gangwal

Reputation: 7937

@LastModifiedBy and @LastModifiedDate not working for embedded object

I am trying to use @LastModifiedDate and @LastModifiedBy annotations to auditing my records in MongoDB.

Here my Model are

    @Document(collection = "A")
    class A{
    @LastModifiedBy
    private String lastModifiedUser;
    @LastModifiedDate
    private Instant lastModifiedDate;
    private List<B> listB;
    }

    class B{
    @LastModifiedBy
    private String lastModifiedUser;
    @LastModifiedDate
    private Instant lastModifiedDate;
    }

Service1.class
public class Service1 {
 public void saveA(A a) {
      repository.save(a)
      }
 }

It's working fine for class A but it's not working with embedded class B.

Any help will be appreciate.

using 2.1.10- Spring Data Mongo and for Springboot 2.1.8.Release

Upvotes: 0

Views: 2979

Answers (1)

shahaf
shahaf

Reputation: 4973

there isn't much on your post to get the behaviour of the final solution you are aiming for... so I'll try my best :)

as the day of this post was written, spring have a known issue with audit of embed document as stated here, so you'll have to override the audit interface with your own logic for setting and changing the lastModifiedUser and lastModifiedDate fields, this is possible but not so easy and may introduce un-desired behaviour to the functionality of spring-data in your project

or

you can force all changes of B by a custom method, so every modification of List<B> will trigger a "change check" (this is not involved any spring-data capabilities)

another approach will be (my suggestion)

use B as a separate document, and store in A the list of b's refs using @DBRef

Upvotes: 1

Related Questions