Affects Version/s: 5.2 M2 (Moore), 5.1.5 (Lovelace SR5)
Fix Version/s: None
We're trying to use auditing features via @EnableNeo4jAuditing. For our entity classes, we have a common base class (abstract because we don't want it to appear in the label hierarchy):
When calling Neo4jSession#save, the Neo4jAuditingEventListener is called automatically as expected and internally calls IsNewAwareAuditingHandler#markAudited, which then calls Neo4jPersistentEntity#isNew to decide whether the entity should be marked as created (AuditingHandler#markCreated) or as modified (AuditingHandler#markModified).
The way SDN is initialised via Neo4jAuditingRegistrar, Neo4jPersistentEntity#isNew uses the IsNewStrategy implemented in Neo4jPersistentEntity.Neo4jIsNewStrategy#isNew which is not working because it makes its decision based on whether the entity has an id assigned - which is happening automatically before any event listener is called (SaveEventDeleage#preSaveCheck -> SaveEventDelegate#visit -> MappingContext#nativeId -> MappingContext#generateIdIfNecessary).
So SDN will always assume that an entity is not new.
We found two workarounds which are neither documented nor nice but I'm adding them here in case anyone else runs into this issue:
- Implement org.springframework.data.domain.Persistable in BaseEntity and provide a custom isNew() implementation there. In that case Neo4jPersistentEntity would use PersistableIsNewStrategy#INSTANCE which is calling the entity's isNew() method.
- Implement a custom AuditingHandler, extend Neo4jAuditingRegistrar to use the custom implementation and implement a custom @EnableNeo4jAuditing annotation.
I would be happy to create a pull request but I'm not sure what's the preferred way to solve the problem:
- Generating the Id later would probably require quite some refactoring around SaveEventDelegate and MappingContext and have other side effects. But I feel that this is the only clean solution within the semantics of SDN where each Repository#save call actually results in a MERGE statement.
- If a @Version property is present in the entity, this field could probably be safely used. This solution is btw the default implementation used in Spring Data's BasicPersistentEntity, which is explicitly overridden in SDN. However that's not applicable to everyone's setup.
- Easier approaches such as checking if createdOn is null also don't work with the semantics of SDN (save = MERGE) as functionality would be only correct if the user loads the entity from the DB before.