Repository navigation
Attach Hibernate 7 proxies created by proxy(id) to the current session - #16484
Conversation
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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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 🚀 New features to boost your workflow:
|
jdaugherty
left a comment
There was a problem hiding this comment.
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.
…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.
|
I went ahead and fixed these 2 minor nits, and will merge this. |
Description
Fixes #16483
With GORM for Hibernate 7,
Domain.proxy(id)returns a proxy that can never be initialized. Accessing any of its properties throwsLazyInitializationException: Could not initialize proxy [...] - no session, even inside a transaction.HibernateProxyHandler.createProxy(Session, Class, Serializable)created the proxy withHibernate.createDetachedProxy(sessionFactory, type, key), which is not associated with any session.This PR obtains the proxy through
GrailsHibernateTemplate.load(type, key)instead, which callsSession#getReferenceon the current session. This matches GORM for Hibernate 5, whereproxy()goes throughhibernateTemplate.load().Domain.load(id)on Hibernate 7 already uses the same path.Tests added to
HibernateProxyHandler7Spec:createProxyreturns a proxy that belongs to the current session and can be initializedDomain.proxy(id)with the default proxy factory returns an uninitializedHibernateProxythat initializes on first property accessBoth tests fail without the fix. The second one reproduces the reported
LazyInitializationException.Contributor Checklist
Issue and Scope
7.0.x): Bug fixes only. No new features or API changes.7.1.x): New features are welcome, but breaking existing APIs must be avoided.8.0.x): Reserved for major changes. Breaking API changes are permitted.Code Quality
./gradlew build --rerun-tasks../gradlew codeStyleand resolved any violations. See Code Style for details.Licensing and Attribution
Documentation