使用Spring Data本机查询

时间:2019-02-08 18:10:07

标签: spring-boot spring-data-jpa nativequery

我是spring boot和spring data jpa的新手。我正在尝试使用本机查询基于从UI接收到的搜索属性执行搜索。

基于searchParam获取的记录应搜索searchParam是否包含在任何指定列中(如本机查询中所述)

我编写了以下代码,但最终收到标题中提到的错误。我试图在stackoverflow中查找响应。但是我相信我已经遵循了许多主题中提到的建议。

在这方面的任何帮助将不胜感激。

下面的代码段

EpicController.java

@CrossOrigin
    @RequestMapping(value="/search", method = RequestMethod.GET)
    public Page<Epic> searchEpicsByProjectIdAndSearchParam(@RequestParam String searchParam, @RequestParam String projectId, Pageable pageable) throws Exception {

        logger.info("Inside searchEpicsByAttributes() based on searchQuery API");

        Page<Epic> results = null;

        try {
            results = epicService.searchEpicsByProjectIdAndSearchParam(searchParam, projectId, pageable);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            throw new Exception("Exception occurred :: " + ex.getStackTrace());
        }

        return results;
    }

EpicService.java(接口)

public interface EpicService {
   Page<Epic> searchEpicsByProjectIdAndSearchParam(String searchParam, String projectId, Pageable pageable);
}

EpicServiceImpl.java

@Override
    public Page<Epic> searchEpicsByProjectIdAndSearchParam(String searchParam, String projectId, Pageable pageable) {

        logger.info(" Inside searchEpicsByProjectIdAndSearchParam() API in EpicServiceImpl");

        return epicRepository.findBySearchParamsAndProjectId(searchParam,projectId, pageable);
    }

EpicRepository.java

@Repository
public interface EpicRepository extends JpaRepository<Issue, String> {

@Query(value = 
            "select i.* from issue i where ("
            + "upper(i.name) like upper('%?1%'))"
            +  "and upper(i.project_id) = upper('%?2%')"
            + "ORDER BY i.name DESC \n-- #pageable\n",
           countQuery = 
            "select count(i.*) from issue i where ("
            + "upper(i.name) like upper('%?1%'))"
            +  "and upper(i.project_id) = upper('%?2%')",
            nativeQuery = true)
    Page<Epic> findBySearchParamsAndProjectId(String name, String projectId, Pageable pageable);
}

例外:

2019-02-08 23:25:21.199  INFO 12556 --- [nio-8080-exec-1] c.a.m.A.controller.ProjectController     :  Inside searchEpicsByProjectIdAndSearchParam() API in EpicServiceImpl
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
        at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:525)
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:209)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
Caused by: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist
        at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:502)
        at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:692)
        at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:181)
        at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)
        at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:141)
        at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61)
        at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:101)
        at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:76)
        at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:161)
        at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:152)
        at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateQuery(AbstractStringBasedJpaQuery.java:81)
        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:202)
        at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:188)

1 个答案:

答案 0 :(得分:1)

在参数,即?1和?2周围跳过单引号“'”。工作查询如下:

"select i.* from issue i where ("
        + "upper(i.name) like upper(%?1%))"
        +  "and upper(i.project_id) = upper(%?2%)"
        + "ORDER BY i.name DESC \n-- #pageable\n",
       countQuery = 
        "select count(i.*) from issue i where ("
        + "upper(i.name) like upper(%?1%))"
        +  "and upper(i.project_id) = upper(%?2%)"