matrei opened a new issue, #16483:
URL: https://github.com/apache/grails-core/issues/16483

   ### Expected Behavior
   
   As with GORM for Hibernate 5, `Domain.proxy(id)` returns an uninitialized 
proxy attached to the current session. The proxy loads its state the first time 
a property is accessed.
   
   ### Actual Behaviour
   
   Accessing any property of the proxy throws, even inside a transaction:
   
   ```
   org.hibernate.LazyInitializationException: Could not initialize proxy 
[app.Book#1] - no session
       at 
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178)
       at 
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:348)
       at 
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.call(ByteBuddyInterceptor.java:45)
       at 
org.hibernate.proxy.pojo.BasicLazyInitializer.invoke(BasicLazyInitializer.java:88)
       at 
org.grails.orm.hibernate.proxy.ByteBuddyGroovyInterceptor.intercept(ByteBuddyGroovyInterceptor.java:88)
       ...
   ```
   
   `HibernateGormStaticApi.proxy(Serializable)` delegates to 
`HibernateProxyHandler.createProxy(Session, Class, Serializable)`, which 
returns `Hibernate.createDetachedProxy(sessionFactory, type, key)`. A detached 
proxy is not associated with any session, so it can never initialize itself.
   
   In GORM for Hibernate 5, `proxy()` goes through 
`AbstractHibernateSession.proxy()`, which calls `hibernateTemplate.load(type, 
key)` and returns a proxy attached to the current session. GORM for Hibernate 7 
originally did the same (`Session#getReference`), but switched to 
`createDetachedProxy` in 1fb626ba18 ("hibernate 7: Partial ByteBuddy 
implementation").
   
   The existing tests that access properties on a proxy 
(`Hibernate7GroovyProxySpec`) switch to `GroovyProxyFactory` first, so the 
default `HibernateProxyHandler` path is not covered.
   
   ### Steps To Reproduce
   
   ```groovy
   class Book {
       String title
   }
   ```
   
   ```groovy
   @Integration
   class ProxySpec extends Specification {
   
       @Rollback
       void 'proxy can be initialized inside a transaction'() {
           given:
           def id = new Book(title: 'Grails').save(flush: true).id
           Book.withSession { it.clear() }
   
           when:
           def book = Book.proxy(id)
   
           then:
           book.title == 'Grails' // LazyInitializationException on Hibernate 
7, passes on Hibernate 5
       }
   }
   ```
   
   ### Environment Information
   
   - JDK 21 (Liberica 21.0.12)
   - Hibernate 7.4.10.Final
   - H2 database
   
   ### Example Application
   
   Found by the integration tests of the 
[grails-logical-delete](https://github.com/grails-plugins/grails-logical-delete)
 plugin, which run against both GORM for Hibernate 5 and Hibernate 7 
(`examples/app1-hibernate7`).
   
   ### Version
   
   8.0.0-RC2
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to