While it's a many-to-many relationship, it gives me a cartesian product result. I would like to get an user object populated with its groups only.
If I use this query without 'fetch' (in lazy mode), when I access the object all groups are loaded.
James Sutherland wrote:I think the reason the spec does not allow the aliasing of join fetches, is that if you join fetch something, the same alias should not be used in the where clause, as it could result in returning corrupt objects.
i.e. in your example if the User had several groups, some of which had id=123 and another had id=456, then your join fetch query will only bring back the one with 123. This will mean the JPA provider will build and return User 'John' with only a single group 123, even though 'John' should have 2 groups, this is invalid data.
To correct this in JPA you must use a normal join in conjunction with the fetch join,
i.e.
This will return the correct result.
If you really only want the one User and one Group back, then you should just query both, without the join fetch,
i.e.
Java Developer
SCJP, SCJD, SCWCD, SCBCD