Parameter in like clause JPQL

后端 未结 8 1271
盖世英雄少女心
盖世英雄少女心 2020-11-27 03:20

I am trying to write a JPQL query with a like clause:

LIKE \'%:code%\'

I would like to have code=4 and find

455
554
646
...


        
相关标签:
8条回答
  • 2020-11-27 04:14

    I don't use named parameters for all queries. For example it is unusual to use named parameters in JpaRepository.

    To workaround I use JPQL CONCAT function (this code emulate start with):

    @Repository
    public interface BranchRepository extends JpaRepository<Branch, String> {
        private static final String QUERY = "select b from Branch b"
           + " left join b.filial f"
           + " where f.id = ?1 and b.id like CONCAT(?2, '%')";
        @Query(QUERY)
        List<Branch> findByFilialAndBranchLike(String filialId, String branchCode);
    }
    

    I found this technique in excellent docs: http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/jpa_overview_query.html

    0 讨论(0)
  • 2020-11-27 04:17

    If you do

    LIKE :code
    

    and then do

    namedQuery.setParameter("code", "%" + this.value + "%");
    

    Then value remains free from the '%' sign. If you need to use it somewhere else in the same query simply use another parameter name other than 'code' .

    0 讨论(0)
提交回复
热议问题