0

I'm encountering a problem in my Spring Boot application when I use the @EnableMongoAuditing annotation.

The application throws a NullPointerException that seems to be related to the auditing mechanism. Interestingly, the issue disappears when I remove the @EnableMongoAuditing annotation, but I'd like to use auditing in my application.

Here's the relevant entity class that I'm working with:

import ..*.model.ContractOverdue;
import *.model.Overdue;
import lombok.Builder;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.HashMap;

@Builder
@Data
@Document(collection = "contract-overdue")
public class MGReceiptContractOverdue {

    @Id
    private String id;
    private String contractRef;

    private HashMap<String, HashMap<Integer, Overdue>> overdueList;

    // Constructor and methods...
}

I don't have any auditing-related fields in my entity class, and the issue only arises when the @EnableMongoAuditing annotation is present in the configuration.

Here's a part of the stack trace of the error:

Caused by: java.lang.NullPointerException: null
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.from(PersistentPropertyPathFactory.java:266) ~[spring-data-commons-2.6.3.jar:2.6.3]
    at org.springframework.data.mapping.context.PersistentPropertyPathFactory.lambda$from$2(PersistentPropertyPathFactory.java:262) ~[spring-data-commons-2.6.3.jar:2.6.3]
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:360) ~[spring-data-commons-2.6.3.jar:2.6.3]

What could be causing this issue, and how can I use the @EnableMongoAuditing annotation without encountering a NullPointerException? Any guidance or suggestions would be greatly appreciated.

Thank you in advance for any help!

Smaillns
  • 2,540
  • 1
  • 28
  • 40
  • [how-to-enable-auditing-for-mongodb-via-annotations-in-spring](https://stackoverflow.com/questions/21991293/how-to-enable-auditing-for-mongodb-via-annotations-in-spring) – Andrei Lisa Aug 29 '23 at 08:44
  • It works with other entities, but the document I mentioned above it throws an exception when enabling mongo auditing – Smaillns Aug 29 '23 at 10:18

1 Answers1

0

I don't understand what exactly happens under the hood, But I was able to fix the problem by changing the type from HashMap to Map

@Builder
@Data
@Document(collection = "contract-overdue")
public class MGReceiptContractOverdue {

    @Id
    private String id;
    private String contractRef;

    private Map<String, Map<Integer, Overdue>> overdueList;

    // Constructor and methods...
}
Smaillns
  • 2,540
  • 1
  • 28
  • 40