Case insensitive Query with Spring CrudRepository
SpringSpring Data-JpaSpring Problem Overview
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);
}
Spring Solutions
Solution 1 - Spring
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.
Solution 2 - Spring
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);
}
Solution 3 - Spring
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();
Solution 4 - Spring
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.