EntityManager.find method does not generate SubselectFetches
Description
Activity
Marco Belladelli November 29, 2024 at 8:00 AM
Hi , thank you for reporting the issue and contributing to Hibernate! I left a few comments on your PR, but I wanted to also discuss the point you raise in the issue here.
I understand where you’re coming from since you’re seeing a difference in behavior when comparing find
with join-fetching in an HQL queries, though these operations have very different semantics intrinsically. I am not sure that the org.hibernate.annotations.Fetch
annotation was designed with single-load operations (i.e. find
) in mind, that might be the reason why we see the different behavior in the first place.
Now, I’m not saying we could not implement it, and your approach looks fairly straightforward apart from a few implementation details we can clean up. But if we choose to go that route, this will introduce a change in how associated collections are fetched when using find
that goes against the expectation Hibernate created until now (a quick test with Hibernate 5 shows it was always ignoring the SUBSELECT
fetching). For this reason, I’ve changed your issue type to an improvement request.
Suppose we have a situation where an entity is joined on the main entity.
When using the find method Subselects are not registered on the joined entity. Whereas if using exactly the same query in HQL/JPQL the Subselects are registered.
For instance the following query generated subselects. If selecting the same MainEntity using find then no subselects are registered.
Testcase and suggested solution in PR#9269.