Reputation: 5243
With Spring CrudRepository Query; I want to select "DeviceType" entities with it's "name" property. But following query select the entitles on case sensitive manner. How I make it case insensitive way. Thanks.
public interface DeviceTypeRepository extends CrudRepository<DeviceType, Integer>, JpaSpecificationExecutor<DeviceType> {
public Iterable<DeviceType> findByNameContaining(String name);
}
Upvotes: 131
Views: 149835
Reputation: 11138
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; orAllIgnoreCase
- 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.
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);
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.
Upvotes: 0
Reputation: 1
In my case adding IgnoreCase
work like this. i would prefer to use list
instead of iterator
public List<ContactEntity> findByNameIgnoreCaseContainingAndUserEntity(String name, UserEntity userEntity);
Upvotes: 0
Reputation: 1072
For those who uses custom JPA query Upper keyword and toUpperCase helps. The following code works for me
return entityManager.createQuery("select q from "table " q where upper(q.applicant)=:applicant")
.setParameter("applicant",applicant.toUpperCase().trim()).getSingleResult();
Upvotes: 2
Reputation: 2560
In my case adding IgnoreCase
did not work at all.
I found that it is possible to provide options for the regular expression ,as well:
@Query(value = "{'title': {$regex : ?0, $options: 'i'}}")
Foo findByTitleRegex(String regexString);
The i
option makes the query case-insensitive.
Upvotes: 2
Reputation:
The following Spring data mongo query works for me. I would prefer to use List
instead of Iterator
public interface DeviceTypeRepository extends CrudRepository<DeviceType,Integer>, JpaSpecificationExecutor<DeviceType> {
List<DeviceType> findByNameIgnoreCase(String name);
}
Upvotes: 13
Reputation: 6811
Exactly as @Peter mentioned in the comment, just add IgnoreCase
:
public interface DeviceTypeRepository
extends CrudRepository<DeviceType, Integer>, JpaSpecificationExecutor<DeviceType> {
public Iterable<DeviceType> findByNameContainingIgnoreCase(String name);
}
See documentation for a list of all supported keywords inside method names.
Upvotes: 259