I have the following mapping
1 User can have 0 or more roles.
Query
from User u JOIN Fetch u.roles
If User1 has two roles RoleA and RoleB.
Then User1 is returned twice.
What i expect is User1 should be returned Once with list of roles containing RoleA and RoleB
How can I fix this.
Also please explain behavior for Many To Many relationship.
See the JPA spec 4.4.5.3
SELECT d FROM Department d LEFT JOIN FETCH d.employees WHERE d.deptno = 1
A fetch join has the same join semantics as the corresponding inner or outer join, except that the related objects specified on the right-hand side of the join operation are not returned in the query result or otherwise referenced in the query. Hence, for example, if department 1 has five employees, the above query returns five references to the department 1 entity.
Options
DISTINCT
to the SELECT
clause to filter out the
duplicate rows. EntityGraph
for the query and add the roles
field to it,
and it will be fetched, meaning you omit the "FETCH JOIN"
from the query.roles
field as EAGER, but this would then apply to all fetching of that field, so likely not desirable.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With