Skip to content

HIbernate proxies are not unwrapped for lazy fetched inherited domain objects #1468

@cip123

Description

@cip123

Hi, we are working on updating an application from Grails 2.5.6 to Grails 4.0.10 and the automatic unwrapping of proxies seem to no longer work for inherited classes.

This seems to be an old issue which was first reported here, #1072 . It happened because this line was commented.

#/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java#L2686

 HibernateUtils.handleLazyProxy(domainClass, grailsProperty);

The issue was fixed with this commit grails/grails-data-hibernate5@5c4e6ed

But then it resurfaced, the same line got commented again https://github.com/grails/gorm-hibernate5/blob/9512dbcb91164776acabf128401f235c93b493be/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java#L2686

Steps to Reproduce

Given the domain classes :

class Event  {
  Person person
}

class Person {
}

class Adult extends Person {
    isMarried() {
        return true
    }
}

Where event.person is a lazy loaded property.

Call event.person.isMarried()

Expected Behaviour

Grails should cast unwrap the Proxy (which wraps an Adult class) before calling the method. This was the actual behavior in grails 2.5.6

Actual Behaviour

Grails throws java.lang.ClassCastException: com.aaa.domain.Person$HibernateProxy$fn2kTwKv cannot be cast to com.aaa.domain.Adult

Environment Information

  • Operating System: MacOS 11.3.1
  • GORM Version: 7.0.8
  • Grails Version (if using Grails): 7.0.4
  • JDK Version: 1.8.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions