Proper Usage and Best Practices of LIKE Queries in Spring Data JPA

Nov 24, 2025 · Programming · 11 views · 7.8

Keywords: Spring Data JPA | LIKE Queries | @Query Annotation | Query Derivation | JPQL

Abstract: This article provides an in-depth exploration of common issues and solutions for LIKE queries in Spring Data JPA. Through analysis of practical cases, it explains why LIKE '%place%' queries return no results while LIKE 'place' works perfectly. The article systematically covers the correct usage of @Query annotation, Spring Data JPA's query derivation mechanism, and how to simplify query development using keywords like Containing, StartsWith, and EndsWith. Additionally, it addresses advanced features including query parameter binding, SpEL expressions, and query rewriting, offering comprehensive guidance for implementing LIKE queries.

Problem Background and Phenomenon Analysis

During Spring Data JPA development, many developers encounter issues with LIKE queries not functioning properly. Specifically, when using the LIKE '%place%' pattern for fuzzy searches, queries return empty results, while LIKE 'place' for exact matches works correctly. This seemingly contradictory behavior actually stems from insufficient understanding of Spring Data JPA's query mechanisms.

Core Problem Analysis

The root cause lies in the correct syntax for LIKE expressions within the @Query annotation. In JPQL queries, wildcard characters % must be directly included in the query string rather than passed through parameters. Incorrect approach:

@Query("Select c from Registration c where c.place like :place")
List<Registration> findByPlaceContaining(@Param("place")String place);

The correct approach should be:

@Query("Select c from Registration c where c.place like %:place%")
List<Registration> findByPlaceContaining(@Param("place")String place);

Spring Data JPA Query Derivation Mechanism

Spring Data JPA provides powerful query derivation capabilities that automatically generate query statements based on method names. For LIKE queries, specific keywords can be used to simplify development:

List<Registration> findByPlaceContaining(String place);

This method completely eliminates the need for the @Query annotation, as Spring Data JPA automatically converts the Containing keyword into a LIKE '%value%' query. Similar derived query keywords include:

Advanced LIKE Expression Features

Spring Data JPA supports the use of advanced LIKE expressions within the @Query annotation. When % characters are used in query definitions, Spring Data JPA recognizes these LIKE delimiters and transforms the query into valid JPQL at runtime.

@Query("select u from User u where u.firstname like %?1")
List<User> findByFirstnameEndsWith(String firstname);

In this example, Spring Data JPA removes the % character from the query and enhances the parameter passed to the method with the appropriate LIKE pattern during query execution.

Parameter Binding and SpEL Expressions

Spring Data JPA supports SpEL expressions to enhance query functionality. For LIKE conditions, it's often necessary to append % to the beginning or end of string-valued parameters. This can be achieved by prefixing or suffixing bind parameter markers or SpEL expressions with %:

@Query("select u from User u where u.lastname like %:#{[0]}% and u.lastname like %:lastname%")
List<User> findByLastnameWithSpelExpression(@Param("lastname") String lastname);

Security Considerations and Character Escaping

When using LIKE conditions with values from untrusted sources, values should be sanitized to prevent them from containing any wildcards that could allow attackers to retrieve more data than intended. For this purpose, the escape(String) method is available in the SpEL context:

@Query("select u from User u where u.firstname like %?#{escape([0])}% escape ?#{escapeCharacter()}")
List<User> findContainingEscaped(String namePart);

With this method declaration, findContainingEscaped("Peter_") will find Peter_Parker but not Peter Parker. The escape character used can be configured by setting the escapeCharacter property of the @EnableJpaRepositories annotation.

Query Rewriting Mechanism

Spring Data JPA provides query rewriting functionality that allows modifications to queries before they are sent to the EntityManager. This is particularly useful for handling complex query scenarios:

@Query(value = "select original_user_alias from User original_user_alias", queryRewriter = MyQueryRewriter.class)
List<User> findByNonNativeQuery(String param);

Best Practices Summary

Based on the above analysis, we summarize best practices for LIKE queries in Spring Data JPA:

  1. Prefer query derivation mechanisms to avoid unnecessary @Query annotations
  2. When @Query is necessary, ensure proper placement of % wildcards
  3. Use character escaping mechanisms for inputs from untrusted sources
  4. Consider using SpEL expressions to enhance query flexibility
  5. Leverage query rewriting functionality for customization in complex scenarios

Performance Optimization Recommendations

When dealing with large datasets, LIKE queries may cause performance issues. Recommendations include:

By deeply understanding Spring Data JPA's query mechanisms and correctly utilizing relevant features, developers can effectively resolve various issues with LIKE queries and build efficient, secure database query functionalities.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.