Spring Data Custom Select Query with Join

April 24, 20191 min read

In this tutorial, I am going to show you how to use JOIN in custom spring JPA queries.

The join is always on the id column.

@Query("select distinct user from User user JOIN user.address addr WHERE addr.id = 1;")
	User getUserById(@Param("addressId") Long addressId);

Multiple Joins

@Query("select distinct user from User user JOIN user.account acct JOIN acct.paymentMethod pm WHERE pm.id = 1;")
	User getUserById(@Param("pmId") Long pmId);