Spring Data JPA also provides options to write custom queries. The following snippet shows a simple example:
@Query("select u from User u where u.name = ?1")
List<User> findUsersByNameUsingQuery(String name);
Important things to note are as follows:
- @Query: The annotation to define queries for repository methods.
- select u from User u where u.name = ?1: Java Persistence Query Language (JPQL) query to be executed. ?1 represents the first parameter.
- findUsersByNameUsingQuery: When this method is called, the query specified is executed with the same name as the parameter.