New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error while saving composite entity having another entity id as null #712
Comments
@vtiwari2134 could you please push this test case to github? see https://github.com/javers/javers/tree/master/javers-spring-boot-starter-sql/src/test/groovy/org/javers/spring/sql |
btw, in your test, employeeEntity is a new object not detached one (before hibernate generates Id for it), right? |
And thanks for quick reply. |
Pull request from your fork of javers repo to this repo would be perfect. Then, I only have to switch to your branch. |
Created pull request - #713 for this issue. |
thnx |
ok, so the problem is that you have different mapping style for hibernate (fields) and for JaVers (beans). |
btw, all pointcuts are defined |
After some digging, mapping style is not a problem. I dont konw the reason yet. |
I triaged a bit and this is what i think -
Since we are generating id it will try to merge it as per the above code. Now the above entity it will treat as detached from session so it will create new entity in session and will save that with the ids. That why the Aspect It should more like -
|
Thanks for the insight! Would you like to create second PR with the fix? |
Sure i will create a PR with the fix. |
PR - #718 |
ok, but tests are failing :) looks like commit isn't called now |
Sorry, I just tested test cases for employee entity object. I will check this tomorrow, busy with some work. :) |
@bartoszwalacik I have added inline description in test-cases where i felt it is required. |
ok, thanks, I will do the code review |
@vtiwari2134 thanks for your excellent contribution, release is on it's way to Central |
@bartoszwalacik Thats great! Happy to contribute. :) Thanks! |
released in 3.11.6 |
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 fromthis.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'
The text was updated successfully, but these errors were encountered: