79099360

Date: 2024-10-17 18:18:43
Score: 1
Natty:
Report link

Both @turo comment and @rob-spoor answer helped me understand what's happening. But I wanted to elaborate a bit more to also describe what happens when we add a where clause.

Doing query.from doesn't overwrite the previouse query.from but it adds a new table to the from using the cross join. For example, starting from 1 root to n roots, this is the equivalent sql query:

In my example I had a table with 3 rows.

Nice! But what happens when i add the where clause? In this case the where applies only to the first table. Let's see it in practice:

But what happens when we do just a simple select and not the count?

Apparently JPA implicitly applies a DISTINCT on the primary key. That's why, no matter how many roots we have, the result is always the same as if we only had one root.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @turo
  • User mentioned (0): @rob-spoor
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: zcb