1

I'm using Spring Data MongoDb for backend storage and want to enable mongo auditing and it doesn't work not sure why.

I have next configuration:

@Configuration
@EnableMongoRepositories(basePackages = {"base.package"})
@EnableMongoAuditing
public class DomainConfig {

}

And I have few entities that extend my base class:

@Getter
public class Entity implements Persitable {

    @Id
    @Setter
    protected String id;

    @CreatedDate
    @Setter
    protected Long creationTime;

    @LastModifiedDate
    protected Long modificationTime;

    @Version
    @Setter
    protected Long version;

    @JsonIgnore
    public boolean isNew() {
        return `getVersion() == 0` && getCreationTime() == null;
    }
}

But when I'm calling entityRepository.save(entity) I'm getting NullPointerException on line with getVersion() == 0 and getVersion() returns null but I think it should return 0.

What could be the problem here?

Btw: I'm using Spring Data MongoDb 1.5.2.RELEASE version

Orest
  • 6,548
  • 10
  • 54
  • 84
  • What is `isNew()` used for ? Why is repository's save method accessing this method ? – s7vr Mar 23 '17 at 12:48
  • If you check the code of spring's SimpleMongoRepository you find there line with entityInformation.isNew(entity) and that line calls isNew method of my entity. – Orest Mar 23 '17 at 12:52
  • Yeah I see it. Let me understand this. `AbstractEntityInformation` does have default implementation for `isNew()` and you are trying to provide new implementation based on auditing fields correct ? – s7vr Mar 23 '17 at 13:21
  • Method is not called if your entity is not extending Persistable interface. So I've implemented it and provided new implementation based on auditing fields. – Orest Mar 23 '17 at 13:26
  • Why there is no @ Document annotation. Remove implements Persistable and isNew(). Add @ Document on class and try once. – titogeo Mar 23 '17 at 14:55
  • It has @Document. For now I've fixed it with Long version = 0L; – Orest Mar 23 '17 at 14:59
  • @Orest Did you actually [enable validation](http://stackoverflow.com/a/22583492/5873923)? – Marc Tarin Mar 24 '17 at 08:52
  • @Marc do I need validation for mongo auditing? – Orest Mar 24 '17 at 08:53
  • I should have wrote "Did you try to enable validation?"... I'm not a 100% sure it is needed with the `@LastModifiedDate` and `@CreationDate` validation annotation, but for instance `@NotNull`, `@NotBlank`, `@NotEmpty` requires validation in order to work. It's worth a shot. – Marc Tarin Mar 24 '17 at 08:57

0 Answers0