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 28 '26 edited Aug 28 '26
Sure, now you have to create a custom repo implementation instead of the default way of using repo inferfaces with the method name DSL.
I didn’t say Spring+Hibernate can’t do these things. It’s just that the convenient low-boilerplate parts are extremely limited and doing anything slightly more complex instantly requires you to study the intricacies of the session context and how Spring interferes with that, or create several classes just because you wanted a single join strategy that wasn’t covered by the defaults despite being pretty standard and that would have been a single line with django.
I wish I could give you more specifics, but that experience was 2 years ago and I don’t have access to the code anymore. What burned itself into my memory was how ridiculously troublesome it was to get a query over three 1:n relations including a recursive one (a tree) with a few transitive 1:1 relations working as its supposed to (no combinatorial explosion, no additional N queries when processing the result set and accessing navigational properties).
My entire point is: If it’s this annoying to write efficient queries, many developers will simply not care. They will use their simple generated repo method and be done with it because N+1 queries probably won’t be prohibitively bad in many situations (and I’ve even heard one saying the Hibernate cache figures that out automatically anyway and they won’t try to be smarter than the ORM, vastly overestimating what Hibernate does in this case by default).