Skip to content

#3129 Fix a problem where DBJsonB with enum is considered dirty without any changes#3746

Merged
rbygrave merged 1 commit intoebean-orm:masterfrom
LeComptoirDesPharmacies:bugfix/3129
Apr 12, 2026
Merged

#3129 Fix a problem where DBJsonB with enum is considered dirty without any changes#3746
rbygrave merged 1 commit intoebean-orm:masterfrom
LeComptoirDesPharmacies:bugfix/3129

Conversation

@AntoineDuComptoirDesPharmacies
Copy link
Copy Markdown
Contributor

This Pull Request aim to fix the problem of DBJSONB dirty detection listed in #3129 which was due to PostgreSQL JSONB key reordering while storing value. This cause Ebean to mark @DbJsonB properties as dirty on every load (triggering unnecessary UPDATEs and version increments) because the raw DB JSON key order differed from Jackson's serialization order. Currently, Ebean is using CRC32 Checksum to compare but it is field ordering-dependent.

Introduce JsonContentHash:
A streaming order-independent structural hash of JSON content using Jackson's JsonParser. Object keys are combined with commutative addition (a + b == b + a) so key ordering does not affect the hash, while array elements use positional hashing to preserve semantic ordering. The hash uses FNV-1a for strings and MurmurHash3's fmix64 finalizer (both public domain) for mixing, producing a 64-bit hash with strong avalanche properties.

Changes:

  • Add JsonContentHash utility (streaming, zero allocation, O(n) time)
  • SourceMutableValue: use fast string equality with canonical hash fallback
  • ChecksumMutableValue: replace CRC32 with JsonContentHash (also upgrades collision resistance from 2^32 to 2^64)
  • No API changes, no schema changes, readSet() untouched

This Pull Request aim to fix the problem of DBJSONB dirty detection listed in ebean-orm#3129 which was due to PostgreSQL JSONB key reordering while storing value.
This cause Ebean to mark @DbJsonB properties as dirty on every load (triggering unnecessary UPDATEs and version increments) because the raw DB JSON key order differed from Jackson's serialization order.
Currently, Ebean is using CRC32 Checksum to compare but it is field ordering-dependent.

Introduce JsonContentHash:
A streaming order-independent structural hash of JSON content using Jackson's JsonParser.
Object keys are combined with commutative addition (a + b == b + a) so key ordering does not affect the hash, while array elements use positional hashing to preserve semantic ordering.
The hash uses FNV-1a for strings and MurmurHash3's fmix64 finalizer (both public domain) for mixing, producing a 64-bit hash with strong avalanche properties.

Changes:
- Add JsonContentHash utility (streaming, zero allocation, O(n) time)
- SourceMutableValue: use fast string equality with canonical hash fallback
- ChecksumMutableValue: replace CRC32 with JsonContentHash (also upgrades collision resistance from 2^32 to 2^64)
- No API changes, no schema changes, readSet() untouched
@rbygrave
Copy link
Copy Markdown
Member

I really like this. There could be a minor performance difference but that is kind of moot relative to it not working (bug) and the extra costs of persisting the documents that haven't actually change.

So yeah, great work and I really like this approach.

@rbygrave rbygrave added this to the 16.5.0 milestone Apr 12, 2026
@rbygrave rbygrave merged commit c8a7a26 into ebean-orm:master Apr 12, 2026
1 check passed
@rbygrave rbygrave linked an issue Apr 12, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DbJsonB immediately dirty on load if multiple enum as property

2 participants