Query by Example (QBE) in Spring Data
Query by Example (QBE) is a powerful querying technique provided by Spring Data JPA that allows developers to create dynamic queries without the need to write traditional query language statements. This approach is particularly useful for decoupling application code from the underlying data store APIs and for managing frequent changes in domain object structures.
Components of Query by Example
QBE involves three main components:
-
Probe: A probe is a domain object with certain properties set. It acts as a template for the query, where the set properties define the criteria for the search.
-
ExampleMatcher: This component defines the matching rules for the query. It allows customization of how the properties of the probe are matched against the data store. For instance, it can specify whether string matching should be case-sensitive or whether it should match the start, end, or any part of the string.
-
Example: The Example combines the probe and the ExampleMatcher to generate the query. It encapsulates the criteria defined by the probe and the matching rules specified by the ExampleMatcher.
Benefits and Limitations
QBE is beneficial for applications where domain objects frequently change, as it allows queries to adapt without requiring modifications to the query logic. However, QBE has some limitations, such as only supporting certain types of string matching and not supporting nested or grouped property constraints.
Practical Examples
Example 1: Simple Probe with Last Name
In the following test, a simple probe is created with the lastName property set to "Smith". The query finds all users matching this probe.
User probe = new User(null, null, "Smith"); // #1
List<User> result = userRepository.findAll(Example.of(probe)); // #2
assertThat(result).hasSize(2) // #3
.extracting("username").contains("john", "burk"); // #3
- #1: A
Userinstance is initialized with thelastNameset to "Smith". - #2: The query is executed to find all users matching the probe.
- #3: The test verifies that the query returns two users with usernames "john" and "burk".
Example 2: Using ExampleMatcher
The following tests demonstrate the use of ExampleMatcher to customize query behavior.
@Test
void testEmailWithQueryByExample() {
User user = new User(); // #1
user.setEmail("@someotherdomain.com"); // #1
ExampleMatcher matcher = ExampleMatcher.matching() // #2
.withIgnorePaths("level", "active") // #2
.withMatcher("email", match -> match.endsWith()); // #2
Example<User> example = Example.of(user, matcher); // #3
List<User> users = userRepository.findAll(example); // #4
assertEquals(4, users.size()); // #5
}
- #1: A
Userinstance is initialized with an email ending with "@someotherdomain.com". - #2: An
ExampleMatcheris created, ignoring the "level" and "active" properties and configuring the matcher to find emails ending with the specified domain. - #3: An
Exampleis created to combine the probe and matcher. - #4: The query is executed.
- #5: The test verifies that four users match the criteria.
In another test, a probe is created with the username property starting with "J", and the matcher is configured to ignore case and match starting strings.
@Test
void testUsernameWithQueryByExample() {
User user = new User(); // #6
user.setUsername("J"); // #6
ExampleMatcher matcher = ExampleMatcher.matching() // #7
.withIgnorePaths("level", "active") // #7
.withStringMatcher(ExampleMatcher.StringMatcher.STARTING) // #7
.withIgnoreCase(); // #7
Example<User> example = Example.of(user, matcher); // #8
List<User> users = userRepository.findAll(example); // #9
assertEquals(3, users.size()); // #10
}
- #6: A
Userinstance is initialized with a username starting with "J". - #7: The matcher is configured to ignore case and match starting strings.
- #8: An
Exampleis created. - #9: The query is executed.
- #10: The test verifies that three users match the criteria.
These examples illustrate how QBE can be used to create flexible and dynamic queries in Spring Data applications.
FAQ (Frequently asked questions)
What is Query by Example (QBE)?
What are the components of Query by Example?
What is the role of a probe in Query by Example?
How does Query by Example help in software development?
How does Query by Example work in Spring Data JPA?
What is the purpose of an ExampleMatcher in Query by Example?
What are the limitations of Query by Example?
Why is Query by Example useful in Spring Data JPA?