Skip to content

Error while saving composite entity having another entity id as null #712

@vtiwari2134

Description

@vtiwari2134

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) {
this.onVersionEvent(pjp, this.saveHandler);
}
should evaluate the return entity from 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>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions