Using Query By Example to find tricky answers
So, what happens when the exact criteria for a query vary from request to request? In short, we need a way to feed Spring Data an object that captures the fields we’re interested in while ignoring the ones that we aren’t.
The answer is Query By Example.
Query By Example lets us create a probe, which is an instance of the domain object. We populate the fields with criteria we want to apply and leave the ones we aren’t interested in empty (null
).
We then wrap the probe, creating an Example
. Check out the following example:
VideoEntity probe = new VideoEntity(); probe.setName(partialName); probe.setDescription(partialDescription); probe.setTags(partialTags); Example<VideoEntity> example = Example.of(probe);
The preceding code can be broken down as follows:
- The first few lines are where we create the probe, presumably pulling down fields from a Spring MVC web method where they were posted, some...