JOIN FETCH does not work properly with enhanced entities
Description
Activity
Yoann Rodière June 7, 2021 at 1:19 PM
This was apparently solved at some point during the development of 5.5.
See in particular: https://github.com/hibernate/hibernate-orm/commit/db54fe44dba33389c73ea42bc10c938e0649a028#
A Susnjar March 5, 2021 at 7:01 PM
Please note related discussion at https://discourse.hibernate.org/t/how-to-make-lazy-loading-truly-lazy-v5-4/1994/15
There is a symmetric issue about being able to make those “now lazy” properties eager when required (i.e. via entity graphs or fetches).
Patrice Blanchardie February 5, 2021 at 10:05 AMEdited
I verified the behavior on the above reproducer and it works as I expected.
But, only with bytecode enhancement without enhancement as proxy, ToOne(LAZY)
becomes eager when the same entity is referenced multiple times in a ResultSet (the first record’s ToOne stays lazy, but the others become eager).
In this case, when returnNarrowedProxy
is called, it unwraps and initializes at DefaultLoadEventListener.java#L363, triggering an immediate load.
Here is the reproducer:
I fixed it by applying the fix suggested by Gail.
See https://github.com/hibernate/hibernate-orm/pull/3716/files#r570691715
Steve Ebersole February 4, 2021 at 9:10 PM
I’ve created a PR to address this and HHH-13658. WOuld y’all be willing to give that a try with your use cases?
Patrice Blanchardie January 13, 2021 at 5:00 AM
You're welcome.
I've prepared a reproducer in a repository on github:
https://github.com/fprochazka/hibernate-enhancement-unexpected-queries-bug
I don't understand hibernate internals enough to dig deeper, but this is clearly unwanted, because when I've just enabled it on our project, one of my API endpoints went from 10 queries to 816 queries and I gave up on examining the rest of them.
I've even tried to annotated all of
toOne
relations in our project with different combinations of@LazyToOne
(with differentLazyToOneOption
) and@LazyGroup
in the hopes that I've just misunderstood the feature, but it just went from broken (because of 100x times more queries which is unacceptable) to completely broken (where the data weren't even loading properly).