Entity gets updated when a query is performed. Dirty check changes

Description

I was using hibernate 6.3.1.Final and upgraded to 6.6.2.Final

Here, I noticed that a column annotated with

was triggering a dirty check and an update to the entity when a hql query was executed.

 

This did not happen in 6.3.1.Final

Furthermore, changing the List to a String doesn’t trigger the dirty check

 

Why is this happening in 6.6.2.Final ?

Default flush mode is AUTO

I understand that before a hql is executed a flush is performed but it’s breaking the application as I now get a lot of StaleObjectStateException and OptimisticLockException

Attachments

1

Activity

Show:

Christian Beikov December 17, 2024 at 10:21 AM

We didn't mention this in the migration guide because this was a bug fix. You can search for the Jira issue if you want, but ORM wasn't flushing changes even if the json data was changed before this fix.

Bogdan Marinescu December 3, 2024 at 12:02 PM

can be closed. i don’t have the rights to do that

Bogdan Marinescu December 3, 2024 at 6:40 AM

I see. This explains it

I checked only the migration guide from 6.3 to 6.6 to see if there is something specified about the JsonJavaType and I also saw that the dirtyCheck mechanism was also changed between 6.3 and 6.6 but didn’t look that deep.

Would have been good maybe to have a mention of this in the migration guide as this can cause issues but it’s also my fault for not implementing hashcode and equals.

Thanks for looking into this further.

stringintech December 2, 2024 at 8:54 PM

I looked further and found where the behavior difference comes from. When you first persist the Prescription entity, Hibernate builds an internal snapshot of its state. In 6.3.1, BasicCollectionJavaType was used which preserved reference equality during deep copy (see this), so the snapshot contained the same ValidationMessageError instance. In 6.6.2, JsonJavaType is used which creates new instances during deep copy (see this). Knowing this explains why Hibernate detects "changes" when comparing against the snapshot in 6.6.2, even though the actual data hasn't changed.

Bogdan Marinescu December 2, 2024 at 8:40 AM
Edited

I can confirm that adding a equals doesn’t crash the junit test anymore and hibernate doesn’t consider Prescription as dirty.
When I debugged, the validationMessages from the Invoice query, they came back null so I assumed that it compares null with what is in the database.

Seems that is not the case.
Thanks for looking into this.

 

Although it is strange that hibernate 6.3.1 did not have any issue with this, isn’t it ?

Out of Date

Details

Assignee

Reporter

Labels

Worked in

Affects versions

Priority

Created November 27, 2024 at 1:09 PM
Updated December 17, 2024 at 10:21 AM
Resolved December 17, 2024 at 10:21 AM