-
-
Notifications
You must be signed in to change notification settings - Fork 371
Description
Following exception occurs while saving entity with javers auditing enabled in spring -
JaversException ENTITY_INSTANCE_WITH_NULL_ID: Found Entity instance 'com.test.entity.DepartmentEntity' with null idProperty 'id'
I think the issue is with JaversSpringDataAuditableRepositoryAspect.class
having aspect on save (@AfterReturning("execution(public * save(..)) && this(org.springframework.data.repository.CrudRepository)")
) method. In this the entity is audited on the argument passed to save method but auditing should be performed on return entity that is saved by hibernate.
Issue arises because whenever detached entity is saved to persist hibernate tries to merge the entity creating new entity in hibernate session. Hence the return entity is different from the entity passed to save.
That why the Aspect public void onSaveExecuted(JoinPoint pjp) {
should evaluate the return entity from
this.onVersionEvent(pjp, this.saveHandler);
}save
method.
Sample spring project to replicate bug -
test.zip
To replicate issue run JaversExceptionTest test case. This test class contains 2 test one saves entity without having auditing enabled and one with javers auditing enabled.
In first case EmployeeEntity is saved successfully in db while second case throws error : -
JaversException ENTITY_INSTANCE_WITH_NULL_ID: Found Entity instance 'com.test.entity.DepartmentEntity' with null idProperty 'id'
at org.javers.core.metamodel.type.EntityType.getIdOf(EntityType.java:88)
at org.javers.core.metamodel.object.InstanceId.createFromInstance(InstanceId.java:28)
at org.javers.core.metamodel.object.GlobalIdFactory.createId(GlobalIdFactory.java:49)
at org.javers.core.graph.LiveCdoFactory.create(LiveCdoFactory.java:25)
at org.javers.core.graph.LiveCdoFactory.create(LiveCdoFactory.java:10)
at org.javers.core.graph.EdgeBuilder.buildSingleEdge(EdgeBuilder.java:30)</code>