Named Query with like in where clause

前端 未结 4 1133
无人及你
无人及你 2020-12-29 03:49

Is it possible to have a like in a where clause in a named query? I am trying to do the following but am getting exceptions

@NamedQuery(name = \"Place.getPla         


        
相关标签:
4条回答
  • 2020-12-29 03:58

    If you are using positional parameters, you can use this way

    @Query("select p from ABC p where p.name like CONCAT('%',?1,'%') ")
    
    0 讨论(0)
  • 2020-12-29 04:03

    You can't have the % in the NamedQuery, but you can have it in the value you assign the parameter.

    As in:

    String city = "needle";
    query.setParamter("city", "%" + city + "%");
    
    0 讨论(0)
  • 2020-12-29 04:05

    You can also use CONCAT function

    @NamedQuery(name = "Place.getPlaceForCityAndCountryName",
         query = "SELECT p FROM Place p WHERE " +
            "lower(p.city) like CONCAT(:city,'%')");
    
    0 讨论(0)
  • 2020-12-29 04:05
    @Query("select c from Curso c where c.descripcion like CONCAT(:descripcion,'%')")
    
    List<Curso> findByDescripcionIgnoreCase(@Param("descripcion") String descripcion);
    
    0 讨论(0)
提交回复
热议问题