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:
SELECT COUNT(*) FROM userr;SELECT COUNT(*) FROM userr, userr;SELECT COUNT(*) FROM userr, ..., userr;In my example I had a table with 3 rows.
SELECT COUNT(*) FROM userr; = 3SELECT COUNT(*) FROM userr, userr; = number of rows of the table resulting from the cross join between userr and userr = 3x3 = 9SELECT COUNT(*) FROM userr, userr, userr; = 3x3x3 = 27Nice! 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:
SELECT COUNT(*) FROM userr WHERE name = 'josh'; = 2SELECT COUNT(*) FROM userr, userr WHERE name = 'josh'; = take only users which name is 'josh' from the first userr table (there's two joshes), then do the cross join with the second userr table which has all three users = 2x3 = 6SELECT COUNT(*) FROM userr, userr, userr WHERE name = 'josh'; = 2x3x3 = 18But 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.