Wrong results returned from the cache when doing multiple queries returning same result

Description

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

Activity

Show:

Marco Belladelli November 27, 2023 at 4:18 PM

Hi , thank you for reporting the issue and providing a reproducer.

This problem looked very similar to https://hibernate.atlassian.net/browse/HHH-17188, that was already fixed in version 6.3.2.Final. I have confirmed that your test case works correctly with that version as well as the new 6.4.0.Final, closing this as duplicate.

Oleksii Miroshnyk November 14, 2023 at 3:45 PM

Oleksii Miroshnyk November 14, 2023 at 2:54 PM

PR to with test case that demonstrates the issue

Duplicate

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created November 14, 2023 at 2:43 PM
Updated November 27, 2023 at 4:18 PM
Resolved November 27, 2023 at 4:18 PM

Flag notifications