Spring Data JPA queries, by default, are case-sensitive. In other words, the field value comparisons are case-sensitive.
Case insensitive SQL SELECT: Use upper or lower functions select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
CrudRepository mainly provides CRUD operations. PagingAndSortingRepository provide methods to perform pagination and sorting of records. JpaRepository provides JPA related methods such as flushing the persistence context and deleting of records in batch.
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.
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);
}
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With