Spring Data Custom Select Query with Join

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);

Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *