Case insensitive Query with Spring CrudRepository

后端 未结 4 1812
Happy的楠姐
Happy的楠姐 2020-12-04 18:44

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

相关标签:
4条回答
  • 2020-12-04 19:15

    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);
    } 
    
    0 讨论(0)
  • 2020-12-04 19:29

    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.

    0 讨论(0)
  • 2020-12-04 19:29

    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.

    0 讨论(0)
  • 2020-12-04 19:30

    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();
    
    0 讨论(0)
提交回复
热议问题