-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetchgraph does not prevent Hibernate from fetching lazy one-to-one relationships #42425
Comments
Could you attach a zip with the reproducer to your comment (or point to a personal GitHub repo)? Will be easier than a diff. Also I would recommend to try with the latest 3.13.1 to see how it goes there. |
I've both updated the issue with a link to the Git repository and attached an archive. I forgot to mention it, but yes I had reproduced the issue with Quarkus 3.13.1. |
Thanks, but your reproducer serializes the fruit details in the REST endpoint's response, that's why it gets initialized. That being said, the problem you mention does exist, and I managed to change your reproducer to demonstrate that:
In all cases I get something like this, showing the association does get loaded unexpectedly:
I didn't try to find out what's going on on 2.16, because that's Hibernate ORM 5, and that version is known to not handle fetch graphs correctly. However, that was supposed to be fixed in Hibernate ORM 6 (used in Quarkus 3.13), so that's weird. I tried to debug the reproducer a bit, and here is what I found:
That last step looks like a bug, because we already decided that the association shouldn't be initialized (which is correct, according to |
@yrodiere it seems a bug to me, when bytecode enhancement is enabled the one-to-one association should be fecthed lazily, |
Thanks @dreab8 , I reported this as https://hibernate.atlassian.net/browse/HHH-18489 , with a reproducer. |
Thank you, @yrodiere, for improving the reproducer and opening the issue on the Hibernate bug tracker. In the meantime, we developed a fully functional workaround using a tuple query with javax.persistence.criteria.CriteriaBuilder#createTupleQuery. However, this came at the cost of many lines of code and required us to reimplement a mechanism to convert OneToMany relationships from a flat table representation back into a structure of nested Java objects. |
Describe the bug
In a monolith using WebLogic (as application server) and Eclipselink (as ORM), there was a very slow SQL query (that could takes more than 10 seconds) that can returns thousand of rows. We have been able to optimize it so it takes less than 1 second when using a fetch graph. Prior the fix, a mapper after the method
javax.persistence.TypedQuery#getResultList
was submitting dozen of SQL queries (fetching lazy relationships and lazy of lazy...) for each row when using the getters of the main entity class. The fetch graph allowed to eagerly fetch lazy relationships in a single query.Now, the entire feature is planned to be moved into a Quarkus 2 project while still keeping the DDL and the database for iterative migrations. Unfortunately, the fetch graph is not working as expected.
Yet the Hibernate documentation states (source 6, see all links below):
There was a bug which ignored the hint but it seems solved since the 5.4.22: https://hibernate.atlassian.net/browse/HHH-8776
What I tried :
@LazyToOne
annotations (source 1, 5 or 8), but this was required for Hibernate < 5.5.optional = false
for@OneToOne
(source 3 or 4)hibernate.enhancer.enableLazyInitialization
to true inapplication.properties
(source 7)I can't update the DDL, so it prevents using the annotation
@MapsId
(source 3 or 4).According to source 2:
According to source 8:
I don't know why it does not work, can it be related to some internals Quarkus bytecode enhancement incompatible with Hibernate?
Sources :
Expected behavior
I expect only one query in the logs:
Actual behavior
There is an additional request to fetch the lazy
@OneToOne
relationship:How to Reproduce?
Here is a minimal project to reproduce the issue (branch 2.16): https://github.com/KevinSeroux/quarkus-quickstarts/tree/2.16
If you prefer an archive: hibernate-orm-quickstart-fetchgraph.tar.gz
If you only want the diffs from hibernate-orm-quickstart (might be easier to locate the issue): quarkusio/quarkus-quickstarts@2.16...KevinSeroux:quarkus-quickstarts:2.16
I reproduce the issue also with Quarkus 3.13.1.
Output of
uname -a
orver
Linux ITEM-S131685 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 GNU/Linux
Output of
java -version
openjdk version "17.0.11" 2024-04-16 OpenJDK Runtime Environment (build 17.0.11+9-Debian-1deb12u1) OpenJDK 64-Bit Server VM (build 17.0.11+9-Debian-1deb12u1, mixed mode, sharing)
Quarkus version or git rev
2.16
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Additional information
No response
The text was updated successfully, but these errors were encountered: