L1 Cache object updating issue

Description

Description

I'm not an expert in Hibernate, but I don't believe you have to be one when it comes to this particular issue. The issue is that Hibernate L1 cache (session) does NOT update the cached object even though it gets retrieved from the database.

How to reproduce
This will cause a serious issue when entity relations come into the play. Suppose we have a Parent entity that relates to a Child entity with a one-to-many relationship. The parent with an id = 1 has 3 children, one of the children has an id = 1. If we queried the parent table to get the parent with the child of an id=1 as follows:
SELECT u from User u LEFT JOIN FETCH u.childrenList as s WHERE s.id = 1

We will get the parent entity with a childrenList of size = 1. So let's say within the same block of code we needed to get all the other children of the same parent. We will query the database with
SELECT u from User u LEFT JOIN FETCH u.childrenList WHERE u.id = :id

This will result of querying the database with the right query, and all the children of the parent will get retrieved from the database. However, once it reaches the L1 cache, the parent will not get updated and the childrenList will not change. This means we've lost the other children that should be added to the parent childrenList and stuck with the child of id = 1.

I don't actually get it. Why wouldn't the L1 cached entity get updated even though it got retrieved from the database.
I know that I can call entityManager.clear() as a work around for this issue. But why would I do this when we can solve the issue considering we know the root cause of the issue.

Environment

None

Status

Assignee

Unassigned

Reporter

Omar Muhtaseb

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Priority

Critical
Configure