Search code examples
javamongodbjavers

Audit MongoDB Subdocument with Javers


I'm trying to audit changes to a subdocument using Javers, however the subdocument change is not showing up when I use findChanges:

Below is the structure of the document and the subdocument:

{
  "id": "5fafa42d99fa9d005fc288a1",
  "lastModifiedDate": "2020-11-14T09:32:45.118+00:00",
  "workflowState": {
    "isTerminus": false,
    "label": "Under Consideration",
    "entry": false
  },
  "workflowId": "5f9ef05e607b75554aa7e93f",
}

However, when I make changes to this entity using the code below, only the property lastModifiedDate appears in the changes:

WorkflowState newState = workflowService.transitionIssue(issue, transition);

IssueDTO issueDTO = new IssueDTO(
    issue.getId(),
    new Date(), // lastModifiedDate
    newState, // also new but is not getting listed in changes
    issue.getWorkflowId()
);

Issue entity = Issue.fromIssueDto(issueDTO);

issueService.save(entity);

Below is the code I'm using to retrieve the changes:

return javers.findChanges(QueryBuilder.byInstance(issue).build());

Am I missing something here?


Solution

  • It looks like I need to use the method .withChildValueObjects() in the query like so:

    javers.findChanges(
          QueryBuilder
          .byInstance(issue)
          .withChildValueObjects()
          .build()
    );