Trying to test if a LocalDate contains a given year, day or month.
For example, given the criteria "1979", I'd like filter the rows returned by a findAll(Example<S> example)
JPA query in such way that it will return any row containing "1979".
The "problem" is that the entity defines the date (let say a birth date) as a LocalDate
and as such, doing:
ExampleMatcher personFilter = ExampleMatcher.matchingAll()
.withMatcher("birthDate", ExampleMatcher.GenericPropertyMatchers
.contains().ignoreCase());
personPage = repository.findAll(personFilter);
with PersonFilter#birthDate being itself a LocalDate
.
Basically, I would have to have this entity to declare the birth date as a String for the filter to work, but that is not possible as the DB itself store it as a date
.
Is there a way to solve such problem ? Like overriding the way the matching work ?
CodePudding user response:
The short answer is, you don't.
For more advanced use cases and predicate definitions, have a look at the Querydsl support.
The long answer is:
Query by Example (QBE) is a user-friendly querying technique with a simple interface. It allows dynamic query creation and does not require you to write queries that contain field names.
Query by Example is well suited for several use cases:
- Querying your data store with a set of static or dynamic constraints.
- Frequent refactoring of the domain objects without worrying about breaking existing queries.
- Working independently from the underlying data store API.
Query by Example also has several limitations:
- No support for nested or grouped property constraints, such as firstname = ?0 or (firstname = ?1 and lastname = ?2).
- Only supports starts/contains/ends/regex matching for strings and exact matching for other property types.