What is Spring Data JPA query methods and JPQL?
Why Interviewers Ask This
Mid-level Spring Boot roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
Answer
Spring Data JPA provides multiple ways to define data access queries: 1. Derived query methods (from method name): Spring Data parses the method name and generates the JPQL query: List<User> findByLastNameAndFirstName(String lastName, String firstName); List<User> findByAgeBetween(int min, int max); List<User> findByEmailContainingIgnoreCase(String keyword); List<User> findTop5ByOrderByCreatedAtDesc(); Optional<User> findFirstByEmailOrderByIdDesc(String email); boolean existsByEmail(String email); long countByStatus(String status);. Keywords: findBy, readBy, getBy, countBy, existsBy, deleteBy, And, Or, Not, Like, Containing, StartingWith, EndingWith, Between, LessThan, GreaterThan, OrderBy, Top, First. 2. @Query with JPQL: JPQL uses entity class names and field names (not table/column): @Query("SELECT u FROM User u WHERE u.email = :email AND u.status = :status") Optional<User> findActiveByEmail(@Param("email") String email, @Param("status") String status); @Query("SELECT u FROM User u WHERE SIZE(u.orders) > :minOrders") List<User> findUsersWithManyOrders(@Param("minOrders") int min);. 3. @Query with native SQL: @Query(value = "SELECT * FROM users WHERE full_text_search(?) > 0", nativeQuery = true) List<User> searchUsers(String term);. 4. @Modifying: for UPDATE/DELETE queries: @Modifying @Transactional @Query("UPDATE User u SET u.lastLogin = CURRENT_TIMESTAMP WHERE u.id = :id") int updateLastLogin(@Param("id") Long id);. 5. Projections: return partial data as interface or DTO: interface UserNameEmail { String getName(); String getEmail(); } List<UserNameEmail> findByStatus(String status);. 6. Specification: dynamic queries with Criteria API.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Spring Boot answers easy to follow.
Previous
What is Spring Boot caching with @Cacheable?
Next
What is Spring Boot REST API best practices?