[JIRA] (HHH-15737) Support for Inheritance and Bytecode Enchanced Lazy Loading
by Alessandro Moscatelli (JIRA)
Alessandro Moscatelli ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5ad61be... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMGI2N2JlNzI1... ) / New Feature ( https://hibernate.atlassian.net/browse/HHH-15737?atlOrigin=eyJpIjoiMGI2N2... ) HHH-15737 ( https://hibernate.atlassian.net/browse/HHH-15737?atlOrigin=eyJpIjoiMGI2N2... ) Support for Inheritance and Bytecode Enchanced Lazy Loading ( https://hibernate.atlassian.net/browse/HHH-15737?atlOrigin=eyJpIjoiMGI2N2... )
Change By: Alessandro Moscatelli ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5ad61be... )
Hi everybody !
I understand that at the moment you decided to resort to HibernateProxy when lazy loading an Entity that may be instance of a superclass or of a subtype.
This is to completely avoid hitting the database (and to be completely lazy).
Well, I believe this behaviour should be configurable, especially when we enable bytecode enhancement, since this breaks code based on “instanceof” predicates, getClass(), or APIs that use unproxofied unproxified classes like jakarta.persistence.metamodel.MetaModel.entity and many other.
I am suggesting introducing a configuration to be lazy on everything with the exception of the type information.
Example of what I am proposing :
I am using bytecode enhancement, when Hibernate retrieves a Person, that may also be a NaturalPerson or a LegalPerson, it should make a 'minimal' hit on the database to retrieve the type information (according to the inheritance strategy, all of them should be supported) and return the instance accordingly instead of a proxy. Then any other lazy behaviour on Person type or subtype on any property can occur as before.
The same happens for entities having a polymorphic ToOne relation. Credential is bytecode enhanced and has a ToOne relation with Person. When Hibernate retrieves a Credential, the Person property is set to null. When I call the getPerson() getter method, Hibernate makes the minimal query to determine the type information (according to the inheritance strategy, all of them should be supported), and return an instance accordingly.
I think the same could be applied to the ToMany relations, but I am not an expert here since I usually avoid to use/navigate using this kind of relations.
This behaviour can be summarized with “Lazy Loading with Eager Type Information”.
Of course this makes more hits than actual behaviour, this is why I believe this could be activated with a custom hibernate property, to determine what the user really wants.
Of course, queries to determine the type information should really be minimal, they should involve only the necessary columns and external ids in case of JOINED strategy.
The rationale of this feature request is that on old versions of Hibernate, 5.3 for sure, when using bytecode enhacement and lazy loaded polymorphic relations they defaulted to EAGER instead of using proxies.
But the new behaviour can break projects, like mine, preventing the upgrade.
What do you think ?
I hope I explained myself.
( https://hibernate.atlassian.net/browse/HHH-15737#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15737#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100210- sha1:9b34d7c )