Query Methods or Finder Methods
Query Methods or Finder Methods
Query Methods or Finder Methods
select id, name, description, active, image_url, price, sku from products where name=“product name”
Method parameters
Query method
Subject keyword
2. If we want to limit the number of returned query results, we can add the First or the Top
keyword before the rst By word
Examples: ndFirstByName, readFirst2ByName, ndTop10ByName
3. If we want to select unique results, we have to add the Distinct keyword before the rst
By word.
Examples: ndDistinctByName or ndNameDistinctBy
fi
fi
fi
fi
fi
.
fi
fi
.
fi
fi
fi
fi
Returning Values From Query Methods
A query method can return only one result or more than one result.
1. if we are writing a query that should return only one result, we can return the following types
• Basic type. Our query method will return the found basic type or null
• Entity. Our query method will return an entity object or null
• Guava / Java 8 Optional<T>. Our query method will return an Optional that contains the found
object or an empty Optional.
2. if we are writing a query method that should return more than one result, we can return the
following types
• List<T>. Our query method will return a list that contains the query results or an empty list
• Stream<T>. Our query method will return a Stream that can be used to access the query results or
an empty Stream.
:
fi
Query method - Find by single eld name
Write a query method to nd or retrieve a product by name
fi
Query method - Find by GreaterThan
Write a query method to nd or retrieve products whose price is greater
than given price as method parameter
fi
Query method - Find by LessThan
Write a query method to nd or retrieve products whose price is less
than given price as method parameter
fi
Query method - Find by Containing
Write a query method to nd or retrieve ltered products that match
the given text ( contains check)
fi
fi
Query method - Find by Like
Write a query method to nd or retrieve products for a speci ed pattern
in a column ( SQL LIKE condition)
fi
fi
Query method - Between
Write a query method to nd or retrieve products based on the price
range ( start price and end price)
fi
Query method - Between
Write a query method to nd or retrieve products based on the start
date and end date
fi
Query method - In
Write a query method to nd or retrieve products based on multiple
values ( specify multiple values in a SQL where clause)
fi
Query method - Limiting Query Result
Spring Data JPA supports keywords ' rst' or 'top' to limit the query
results
Example: ndFirstByName(), ndTop5BySku(
An optional numeric value can be appended after 'top' or ' rst' to limit
the maximum number of results to be returned (e.g. ndTop3By....). If
this number is not used then only one entity is returned.
fi
fi
fi
fi
)
fi
fi
s
fi