How do derived query methods work in Spring Data JPA? स्प्रिंग डेटा JPA में डिराइव्ड क्वेरी मेथड्स कैसे काम करते हैं?
Derived query methods let Spring Data generate a query automatically by parsing the method name. Keywords like findBy, And, Or, Between, LessThan, OrderBy, and Containing are mapped to SQL/JPQL clauses, so no query needs to be written manually for common lookups.
Spring parses the method name at application startup, validates it against the entity's fields, and builds the corresponding JPQL query, failing fast at boot time if a referenced field doesn't exist rather than at runtime.
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByEmailAndStatus(String email, String status);
List<User> findByAgeGreaterThanOrderByNameAsc(int age);
boolean existsByEmail(String email);
}डिराइव्ड क्वेरी मेथड्स स्प्रिंग डेटा को मेथड नाम को पार्स करके स्वचालित रूप से क्वेरी जनरेट करने देते हैं। findBy, And, Or, Between, LessThan, OrderBy जैसे कीवर्ड्स को SQL/JPQL क्लॉज़ेज़ में मैप किया जाता है, इसलिए सामान्य लुकअप्स के लिए मैन्युअल रूप से क्वेरी लिखने की ज़रूरत नहीं होती।
स्प्रिंग एप्लिकेशन स्टार्टअप पर मेथड नाम को पार्स करता है, इसे एंटिटी के फील्ड्स के विरुद्ध वैलिडेट करता है, और संबंधित JPQL क्वेरी बनाता है — यदि कोई फील्ड मौजूद नहीं है तो यह बूट टाइम पर ही फेल हो जाता है।
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByEmailAndStatus(String email, String status);
List<User> findByAgeGreaterThanOrderByNameAsc(int age);
boolean existsByEmail(String email);
}Was this answer clear?