Here is the list of all the springboot JPA query keywords so that you make JPA queries easily. Save this and use whenever you build a springboot application.
Basic query keywords
Keyword
Purpose
Example
findBy
Find matching records
findByEmail(String email)
readBy
Read matching records
readByEmail(String email)
getBy
Get matching records
getByEmail(String email)
queryBy
Query matching records
queryByEmail(String email)
searchBy
Search matching records
searchByEmail(String email)
streamBy
Return a stream
streamByEmail(String email)
existsBy
Check whether a record exists
existsByEmail(String email)
countBy
Count matching records
countByUser_Id(UUID userId)
deleteBy
Delete matching records
deleteByUser_Id(UUID userId)
removeBy
Remove matching records
removeByUser_Id(UUID userId)
Example
public interface AppUserRepository extends JpaRepository<AppUser, UUID> {
Optional<AppUser> findByEmail(String email);
boolean existsByEmail(String email);
long countByEmailContaining(String keyword);
}
Derived queries are excellent for straightforward queries, but they aren't the solution for everything. For example, imagine you want: Total monthly income, Category-wise expense breakdown. Here you have to use SQL queries.\
@Query("""
SELECT SUM(t.amount)
FROM Transaction t
WHERE t.user.id = :userId
AND t.transactionType = :type
""")
BigDecimal calculateTotal(
UUID userId,
TransactionType type
);