Containing, Like, StartingWith — String Matching in Spring Data JPA
Fast recall
Quick revision
Core answer
Whether you know which keywords add wildcards automatically and the index/performance impact of a leading %
Passing %text% to Containing (double wildcards) or omitting % with Like and getting an exact match
Mention IgnoreCase for user search and that leading % forces a full scan (StartingWith is index-friendly)
Interview-ready guide
Interview answer
findByNameStartingWith("Ann") commonly produces a prefix LIKE pattern, while findByNameContaining("ann") produces a contains pattern. findByNameLike("Ann%") lets the caller supply the pattern. These methods bind the value; do not concatenate it into @Query text. A normal B-tree can often support a compatible prefix pattern, while contains search may need a database-specific text index.
Containing, Like, StartingWith — String Matching in Spring Data JPA
Direct Answer
Spring Data JPA supports keywords such as Containing, StartingWith, EndingWith, and Like. Containing/StartingWith/EndingWith augment the bound pattern as documented, while Like expects the caller's SQL LIKE pattern. IgnoreCase requests case-insensitive comparison. A leading wildcard usually prevents a normal B-tree prefix seek, but expression, full-text, or trigram indexes can change the plan.
Common Mistakes & Pitfalls
- Adding
%toContaining—findByNameContaining("%phone%")becomesLIKE '%%phone%%'; pass raw text. - Forgetting
%withLike—findByNameLike("john")is an exact match; you must pass%john%. - Skipping
IgnoreCase— "iPhone" won't match "iphone"; users expect case-insensitive search. - Ignoring performance —
Containingon a 10M-row table does a full scan because the leading%defeats indexes.
Check your understanding
Answer these without looking back at the article.
- What are derived query methods in Spring Data JPA?
- How do you use findBy...In() and findBy...Between() in Spring Data?
- How do you limit query results with Top and First in Spring Data JPA?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.