While, I think, answers already provided bring some bits of useful information, I also think that they are lacking.
Spring Data JPA's query building/generation mechanism parses the names of methods that are explicitly (by developer) declared in the custom repository interfaces (which extend CrudRepository<T, ID>
or any of its subtypes) and based on those names, it generates native queries for the corresponding backing datastore/database.
Let's say, we have an managed type (i.e. @Entity) Person
, as follows:
class Person {
@Id
private Integer id;
private String firstname;
private String lastname;
private Integer age;
//getters, setters, constructor, etc.
}
and a corresponding repository, that works with this entity:
interface PersonRepository<Person, Integer> {
}
In order to instruct Spring Data JPA to ignore the case of values provided as arguments to the user-declared repository methods, you can use:
IgnoreCase
- for ignoring case-sensitivity of the specific field; or
AllIgnoreCase
- for ignoring case-sensitivity of all the fields.
Note, that while you should be placing IgnoreCase
right after the field you want to ignore case-sensitivity for, you can place AllIgnoreCase
in almost any place of a method name that you declare.
Ignore case-sensitivity for the specific fields/columns:
Ignores the case of firstname
and lastname
values:
List<Person> findByFirstnameIgnoreCaseAndLastnameIgnoreCase(String firstname, String lastname);
Ignores the case of lastname
value:
List<Person> findByFirstnameAndLastnameIgnoreCase(String firstname, String lastname);
Ignores the case of firstname
value:
List<Person> findByFirstnameIgnoreCaseAndLastname(String firstname, String lastname);
Ignore case-sensitivity for all the fields/columns:
Placing AllIgnoreCase
right before any parameter name:
List<Person> findByAllIgnoreCaseFirstnameAndLastname(String firstname, String lastname);
or after only some parameter name:
List<Person> findByFirstnameAllIgnoreCaseAndLastname(String firstname, String lastname);
or in the end, after all the parameter names:
List<Person> findByFirstnameAndLastnameAllIgnoreCase(String firstname, String lastname);
or even in the very beginning, swapping findBy
with it:
List<Person> AllIgnoreCaseFirstnameAndLastname(String firstname, String lastname);
would all result in the same behaviour - ignoring case-sensitivity for all the fields/columns when generating query.
@Query(value= "select dt from DeviceType as dt where lower(dt.name) like lower(:name)") public Iterable anyMethodNameGoesHere(@Param(name) String name);
– Java_Fire_Within Dec 01 '17 at 20:00