I believe there is a bug with cache integration.
Let’s see on the following test case:
Turn on caching and use ehcache provider
Let’s persist some entity with 2 fields, bool & string
Now let’s do hql select a from EntityA where a.name like ‘xxx’ and give hints to cache this query
results returned as expected. Now 2nd level cache has entry in query-cache, also entity is in 1st level cache entity manager persistence context
now lets do one more hql select a from EntityA where a.someBoolFlag=false
results returned as expected - the same entity as from the first query is returned and all fields of this entity is populated. Now 2nd lvl cache has another entry in the query-cache, !!BUT!! that entry has only id populated.
Now, clear entity manager and do the same query(the 2nd one) again. Results returned are strange: entity is returned but only id is populated. So seems it hits the 2nd level cache but because entity there is “broken“ it simply returns that empty entity.
Seems the issue is that when we do 2nd query, we have entity in the session cache. And so logic only populates a primary key and the rest of fields are not initialized/populated from result set and so when it “finishes up“ and pushes result to 2nd lvl cache it has only if populated.
Guess the step where it identifies that entity is in the session cache should be enhanced with disassembling that entity and emulate the process like it is done when it goes the very first time without any cache data present
Hibernate issue with PR to reproduce