I'm not quite sure if this is Spring or Hibernate issue.
I'm trying to upgrade Spring Boot dependency to 2.2.5.RELEASE (from 2.1.x) in my application and I stuck on JpaObjectRetrievalFailureException. This occurs only when using Hibernate higher than 5.4.0.Final (default provided by Spring is 5.4.12.Final).
There is one repository call
entityaRepository.save(entityA);
which is generating a suspicious query which in turn causes EntityNotFoundException. Actually query itself is fine, but the binding for where clause uses ID which is not correct for table we're performing query on.
Logs look like following (pay attention to last query, which is same as 1st one but with other ID passed as parameter!):
org.hibernate.SQL : select entityA.id as id1_6_3_... where entityA.id=? and ( entityA.deleted_at is null)
o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [39] //fine - there is such entity with id 39
org.hibernate.SQL : select someentity0_.id as id1_1_0_... where someentity0_.id=? and ( someentity0_.deleted_at is null)
o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [1117] //some additional query, which I understand is fine
org.hibernate.SQL : select entityA.id as id1_6_3_... where entityA.id=? and ( entityA.deleted_at is null)
o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [485] //repeated query with ID of other entity!
and at the end we get exception:
HttpServletRequest: [SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterRequest@4edf176e]], Exception: [class org.springframework.orm.jpa.JpaObjectRetrievalFailureException Unable to find com.***.model.EntityA with id 485; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.***.model.EntityA with id 485]
Strange thing is that application is trying to use value 485 as entityA id (because there is no such entity and and no such entity of this type is being created). Id 485 comes from db sequence and belongs to different entity (let's say EntityB) which is being created during the request. I have no clue where the extra query comes from and why the select from EntityA is using id 485. For me either select from EntityA where id = 39 or select from EntityB where id = 485 would be fine.
If I downgrade Hibernate to 5.4.0.Final or lower (like 5.3.15.Final) I see no problem, so my current workaround is to provide additional maven property:
<hibernate.version>5.4.0.Final</hibernate.version>
Comment From: wilkinsona
Thanks for the report. Given that the behaviour changed in a maintenance release of Hibernate (5.4.1.Final), I think it's very likely that this is a Hibernate problem. To be able to spend some time investigating and helping you to determine if that's the case, we'd need a complete and minimal sample that reproduces the problem.
Comment From: jmayday
I understand that providing minimal sample/tests should be preferred way but I'm afraid that it would be quite complicated (as application is complicated - there are some entity managers calls, there are some @query annotated SQL's etc) and I won't have time to do this. I'll try to create ticket in Hibernate Jira.
Comment From: wilkinsona
Understood. Unfortunately, we can't afford the time to try and guess how to recreate a problem, particularly when it looks as if the problem is in another project, so I am going to close this issue. If you have the time in the future to provide a sample, then we can take another look. If you open a Hibernate issue, please comment here with a link to it so that we and anyone else who is interested can follow along. FWIW, I suspect the Hibernate team will need some code that reproduces the problem as well.