Skip to content

Attach Hibernate 7 proxies created by proxy(id) to the current session - #16484

Merged
jdaugherty merged 2 commits into
8.0.xfrom
fix/hibernate7-proxy-attached-to-session
Oct 2, 2026
Merged

jdaugherty merged 2 commits into
8.0.xfrom
fix/hibernate7-proxy-attached-to-session

Conversation

@matrei

@matrei matrei commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #16483

With GORM for Hibernate 7, Domain.proxy(id) returns a proxy that can never be initialized. Accessing any of its properties throws LazyInitializationException: Could not initialize proxy [...] - no session, even inside a transaction.

HibernateProxyHandler.createProxy(Session, Class, Serializable) created the proxy with Hibernate.createDetachedProxy(sessionFactory, type, key), which is not associated with any session.

This PR obtains the proxy through GrailsHibernateTemplate.load(type, key) instead, which calls Session#getReference on the current session. This matches GORM for Hibernate 5, where proxy() goes through hibernateTemplate.load(). Domain.load(id) on Hibernate 7 already uses the same path.

Tests added to HibernateProxyHandler7Spec:

  • createProxy returns a proxy that belongs to the current session and can be initialized
  • Domain.proxy(id) with the default proxy factory returns an uninitialized HibernateProxy that initializes on first property access

Both tests fail without the fix. The second one reproduces the reported LazyInitializationException.

Contributor Checklist

Issue and Scope

  • This PR is linked to an existing issue that has been acknowledged or approved by the project team. If no approved issue exists, please give background on why this change is necessary. Tickets are preferred for release change log history.
  • This PR addresses the complete scope of the linked issue. Partial implementations or unfinished work should not be submitted for review.
  • This PR contains a single, focused change. Unrelated changes should be submitted as separate pull requests.
  • This PR targets the correct branch for the type of change:
    • Patch release branches (e.g., 7.0.x): Bug fixes only. No new features or API changes.
    • Minor release branches (e.g., 7.1.x): New features are welcome, but breaking existing APIs must be avoided.
    • Major release branches (e.g., 8.0.x): Reserved for major changes. Breaking API changes are permitted.

Code Quality

  • I have added or updated tests that cover the changes introduced in this PR. All code contributions are expected to include appropriate test coverage.
  • I have verified that all existing tests pass by running ./gradlew build --rerun-tasks.
  • My code follows the project's code style guidelines. I have run ./gradlew codeStyle and resolved any violations. See Code Style for details.
  • This PR does not include mass reformatting, style-only changes, or large-scale refactoring unless it was explicitly approved in the linked issue. Unsolicited reformatting will not be accepted.
  • If generative AI tooling was used in preparing this contribution, a quality model was used to ensure contributions are consistent with the project's quality standards.

Licensing and Attribution

Documentation

  • If this PR introduces user-facing changes, I have included or updated the relevant documentation.
  • If this PR adds a new feature, I have updated the What's New section of the Grails Guide.
  • If this PR introduces breaking changes or changes that require user action during an upgrade, I have updated the Upgrade Notes for the corresponding version in the Grails Guide.
  • The PR description clearly explains what was changed and why.

HibernateProxyHandler.createProxy() returned a detached proxy
(Hibernate.createDetachedProxy), which is not associated with any session.
Accessing a property of a proxy obtained with Domain.proxy(id) therefore
always failed with a LazyInitializationException, even inside a
transaction.

Obtain the proxy through GrailsHibernateTemplate.load() instead, which uses
Session#getReference on the current session, matching the behavior of GORM
for Hibernate 5.
@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.1516%. Comparing base (9a2291b) to head (c29349d).
⚠️ Report is 15 commits behind head on 8.0.x.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16484        +/-   ##
==================================================
+ Coverage     59.0398%   59.1516%   +0.1117%     
- Complexity      24551      24605        +54     
==================================================
  Files            2175       2170         -5     
  Lines          107668     107610        -58     
  Branches        19618      19606        -12     
==================================================
+ Hits            63567      63653        +86     
+ Misses          35220      35103       -117     
+ Partials         8881       8854        -27     

see 27 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.

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks right to me. proxy() now goes through Session#getReference, the same path as load() on Hibernate 7 and proxy() on Hibernate 5. I reverted the HibernateProxyHandler change locally and both new features fail, so they cover the bug.

One existing test needs updating. HibernateGormStaticApiSpec."proxy test" has a comment saying the Hibernate 7 proxy "may be initialized during retrieval, so we don't assert !isInitialized". That described the detached-proxy behavior this PR removes, and the test passes with or without the fix. Could you remove the comment and assert the actual behavior: the proxy is uninitialized after Club.proxy(entityId), and same.name == 'test' loads it? manager.session.clear() alone is enough there to get an uninitialized proxy.

FYI: with no session bound, proxy() now throws DataAccessResourceFailureException when called, the same as load(). Before, it returned a proxy that could never load. I think that is the right behavior.

@jdaugherty jdaugherty added this to the grails:8.0.0 milestone Oct 2, 2026
…aticApiSpec

The comment in the proxy test described the detached-proxy behavior that
this branch removes. Assert instead that Club.proxy(id) returns an
uninitialized proxy and that the first property access loads it.
@jdaugherty

Copy link
Copy Markdown
Contributor

I went ahead and fixed these 2 minor nits, and will merge this.

@jdaugherty
jdaugherty merged commit 813221e into 8.0.x Oct 2, 2026
81 checks passed
@jdaugherty
jdaugherty deleted the fix/hibernate7-proxy-attached-to-session branch October 2, 2026 16:10
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.

GORM for Hibernate 7: Domain.proxy(id) returns a detached proxy that throws LazyInitializationException when accessed

2 participants