Reputation: 2086
@Query("select u from User u join u.favoriteRestos s where u.id = ?1 ")
User findByIdWithFavoriteRestos (Long userId);
O have this query in the my repo, but instead of returning a User with empty favoriteRestos collection is returning a null user
that returns also a null user:
@Query("select u from User u join fetch u.favoriteRestos s where u.id = ?1 ")
User findByIdWithFavoriteRestos (Long userId);
I also tried with:
@Query("select u from User u left join u.favoriteRestos s where u.id = ?1 ")
User findByIdWithFavoriteRestos (Long userId);
Upvotes: 1
Views: 3254
Reputation: 26046
It doesn't find any user, because join
is an inner join. You need to add left
keyword:
@Query("select u from User u left join fetch u.favoriteRestos s where u.id = ?1 ")
User findByIdWithFavoriteRestos (Long userId);
P.S.: fetch
is also needed if you have a default (lazy) to-many mapping if you want to populate the collection.
Upvotes: 2