Skip to content

Start @ManagedEntity instance id at 0 for Hibernate 7 - #16424

Merged
jdaugherty merged 1 commit into
apache:8.0.xfrom
matrei:fix/hibernate7-managed-entity-instance-id
Sep 29, 2026
Merged

jdaugherty merged 1 commit into
apache:8.0.xfrom
matrei:fix/hibernate7-managed-entity-instance-id

Conversation

@matrei

@matrei matrei commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #16423

Problem

HibernateEntityTransformation added the $$_hibernate_instanceId field with an initial value of -1. Hibernate 7 asserts that an entity not yet associated with a persistence context has an instance id of 0 (EntityEntryContext#addEntityEntry). Saving a new @ManagedEntity instance therefore failed with an AssertionError whenever JVM assertions were enabled. Gradle Test tasks enable them by default, so any integration test whose application saved such an entity at startup failed to load its context.

With assertions disabled, Hibernate overwrites the value through $$_hibernate_setPersistenceInfo(...) as soon as the entity is associated, so production behaviour was not visibly affected.

Change

The field is now added without an initial value, so it starts at 0. This matches Hibernate's own bytecode enhancer and the other fields the transformation adds.

Tests

  • HibernateEntityTransformationSpec: a new instance returns 0 from $$_hibernate_getInstanceId() and has no entity entry or linked managed entities.
  • New ManagedEntitySaveSpec: saves a single @ManagedEntity instance, and several in one session, then reads them back.

With the -1 initialiser restored, all three new features fail: the two save features with the AssertionError from EntityEntryContext.addEntityEntry, and the transformation feature on the instance id.

Verification

  • :grails-data-hibernate7-core:test: 3296 tests, 0 failures (29 skipped)
  • CodeNarc and Checkstyle for grails-data-hibernate7-core: pass

The Hibernate 5 transformation has no instance id field and is not affected.

HibernateEntityTransformation initialised $$_hibernate_instanceId to -1.
Hibernate 7 asserts that an entity not yet associated with a persistence
context has an instance id of 0, so saving a new @ManagedEntity instance
failed with an AssertionError whenever JVM assertions were enabled, which
Gradle test tasks do by default.

Add the field without an initial value, matching Hibernate's own bytecode
enhancer and the other fields added by the transformation.

Fixes apache#16423
@codecov

codecov Bot commented Sep 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.5047%. Comparing base (12be92b) to head (c262e97).

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16424        +/-   ##
==================================================
+ Coverage     58.4944%   58.5047%   +0.0103%     
- Complexity      24184      24188         +4     
==================================================
  Files            2175       2175                
  Lines          107176     107176                
  Branches        19515      19515                
==================================================
+ Hits            62692      62703        +11     
+ Misses          35641      35627        -14     
- Partials         8843       8846         +3     

see 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Sep 29, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: c262e97
▶️ Tests: 88674 executed
⚪️ Checks: 82/82 completed


Learn more about TestLens at testlens.app/docs.

@jdaugherty
jdaugherty merged commit aa0b6c4 into apache:8.0.x Sep 29, 2026
85 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

@ManagedEntity transform initializes $$_hibernate_instanceId to -1, failing Hibernate 7 assertion on persist

2 participants