Case insensitive Query with Spring CrudRepository

SpringSpring Data-Jpa

Spring 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChannaView Question on Stackoverflow
Solution 1 - SpringRoadrunnerView Answer on Stackoverflow
Solution 2 - Springuser5268786View Answer on Stackoverflow
Solution 3 - SpringDapper DanView Answer on Stackoverflow
Solution 4 - SpringrocksteadyView Answer on Stackoverflow