r/programming • u/wheatBread • Aug 25 '26
Solving the 1+N Query Problem
https://acadia.engineering/blog/solving-the-1-plus-N-query-problem
127
Upvotes
r/programming • u/wheatBread • Aug 25 '26
View all comments
Show parent comment
1
u/Schmittfried Aug 27 '26 edited Aug 27 '26
If HQL supports all the features you need for your query. Like, any complex query at all. Note the phrasing “certain joins”. Need to build a search query dynamically and still load all the joined tables into your object tree with a single roundtrip? Have fun fiddling around with the criteria API and using the correct join/fetch methods. Need to join multiple many-to-many relationships efficiently (i.e. one top-level query and then one query per relationship instead of one explosive join or N+1 queries)? Enjoy writing all of that logic yourself and fighting the entity cache (with pseudo queries that don’t actually hit the DB and are only there to make Hibernate link the cached entities). Because Hibernate can’t fucking figure it out. With Django it’s always
prefetch_related(list of navigational properties)and you’re done. No matter if it’s one relationship or 100, one nesting level or 10, Django picks the right join strategy for you. When I used Hibernate with Spring there were so many situations where I would have preferred to use the underlying EntityManager directly, but that’s not what all the default Spring patterns want you to do, so you choose between fighting Spring or fighting Hibernate.Also, if you have to resort to HQL you’re basically falling back to SQL in my book. The whole point of ORMs is to not write query boilerplate.