Let's say I have two tables Posts
and Authors
. The relationship between them is defined as ManyToOne
with EAGER
fetch type.
There is findByNameAndAuthorName
query method to get specific Post
entity. The method is annotated with @QueryHints({ @QueryHint(name = "org.hibernate.cacheable")
which identifies the usage of Hibernate Query Cache
.
When running integration tests, I am getting the javax.persistence.EntityNotFoundException: Unable to find Post with id 77777
for the random tests that verify logic that uses the mentioned JPQ query method above.
Since the Posts
and Authors
data is static, it is pre-inserted into the DB (MS SQL) before running ITs. I noticed that some tests (added by other folks) do CLEAN_INSERT (that is completely remove the pre-inserted static data), therefore I assumed that the exception is thrown because of this.
To prove my theory, I did the following:
- Spin up DB with pre-inserted static data and run the application
- Invoke logic that uses JPA query method that uses Query Cache.
- Manually delete the corresponding record from the
Posts
table. - Invoke the logic with Query Caching again.
As a result, I received the same exception, which confirmed my assumptions.
Question: Can anyone explain to me how Hibernate becomes aware that the cached entity does not exist anymore (or is invalid) if it was removed outside the SessionFactory
life-cycle?
Thanks