Skip to content

[OPENJPA-3104] Do not update an unchanged unenhanced instance on flush - #335

Merged
solomax merged 3 commits into
masterfrom
OPENJPA-3104
Dec 4, 2026
Merged

solomax merged 3 commits into
masterfrom
OPENJPA-3104

Conversation

@rzo1

@rzo1 rzo1 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/OPENJPA-3104

After a JPQL or native bulk DELETE removed the row of a managed, unchanged unenhanced (runtime-subclassed) instance, the next flush issued an UPDATE that hit 0 rows and failed with an OptimisticLockException. Found with Jakarta Data deleteBy methods in TomEE 11.

Causes in the dirty check of untracked instances (SaveFieldManager, StateManagerImpl.dirtyCheck):

  • isFieldEqual read primitive fields as null, so every primitive field was always dirty.
  • A new instance has no saved values after its insert, and the fields of a found or queried instance are loaded after the saved copy was taken, so all fields compared as changed.
  • The saved values were never refreshed, so a String or Date changed back to an earlier value was not written.

Fix:

  • SaveFieldManager keeps a copy of the values last loaded or written and compares with it (by type, arrays with deepEquals). Mutable values are copied; values that cannot be copied fall back to the previous comparison.
  • StateManagerImpl records the values on load and during flush; rollback, savepoint rollback and refresh drop them.
  • preFlush and BrokerImpl.flushAdditions run the dirty check after the callbacks, so changes made by @PreUpdate and flush listeners are written. A relation, embedded or externalized field changed after its owner's preFlush is written by the next flush, as before.

Behaviour: right after a flush an unchanged unenhanced instance is no longer reported as dirty, as for enhanced types (AbstractUnenhancedClassTest adjusted).

Not covered (OPENJPA-3105, closed as Won't Fix; the gaps are now listed in the Runtime Unenhanced Classes section of the reference guide): OBJECT fields holding non-proxy collections or maps, Serializable values without equals(), values with converters or externalizers that cannot be copied. Rollback, refresh, merge and the flush decision before queries behave as before.

Tests: TestBulkDeleteOfUnenhancedType, 40 tests: the reported scenario after persist, find, query, commit and in a later transaction, for field and property access, with byte[], @Lob and embedded fields; changed and changed-back values across flushes and transactions; own and other entities' @PreUpdate, @PostUpdate, flush listeners and cascaded relations. 16 fail on master.

Compare untracked fields with the values last loaded or written, read
primitives by type, and detect changes made by flush callbacks.

Generated-by: Claude Code (Opus 5.5)
@rzo1
rzo1 marked this pull request as ready for review December 2, 2026 10:14
rzo1 added 2 commits December 2, 2026 11:23
…nced classes

Generated-by: Claude Code (Opus 5.5)
…e DELETE test

Generated-by: Claude Code (Opus 5.5)
@solomax
solomax merged commit 05a0e6b into master Dec 4, 2026
16 checks passed
@solomax
solomax deleted the OPENJPA-3104 branch December 4, 2026 03:11
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.

3 participants