简体   繁体   English

为什么我在Spring Data JPA本机SQL查询中获得此异常? IllegalArgumentException:具有该位置[2]的参数不存在

[英]Why I obtain this exception into a Spring Data JPA native SQL query? IllegalArgumentException: Parameter with that position [2] did not exist

I am not so into Spring Data JPA (that uses Hibernate 4.3.11.Final ) and I have the following situation. 我不太喜欢Spring Data JPA (使用Hibernate 4.3.11.Final ),并且遇到以下情况。

I have this SQL query (that performed directly into MySql works fine), the query simply finds all the hotels registered into the accomodation table that are into a specific range from a selected point: 我有这个SQL查询(直接在MySql中执行可以正常工作),该查询只是找到注册到住宿表中,位于选定点的特定范围内的所有酒店:

SET @fiumicino = GeomFromText('POINT(41.768835 12.247658)'); 

SELECT *
FROM accomodation a
WHERE earth_circle_distance(@fiumicino, a.geographical_position) < 7

So as you can see in the previus code this query have 2 parameters: 因此,如您在先前的代码中所见,此查询具有2个参数:

  • @fiumicino : that is a MySqk Spatial POINT object. @fiumicino :这是MySqk Spatial POINT对象。
  • 7: that simply is a range. 7:这只是一个范围

Ok, it works fine... 好的,它工作正常...

So I tryied to convert it into a native SQL query into my Spring Data JPA repository interface, I have done: 因此,我尝试将其转换为本地SQL查询并转换为Spring Data JPA存储库接口,我已经完成了:

@Repository
public interface AccomodationDAO extends JpaRepository<Accomodation, Long> {

    @Query(value = "SELECT * FROM accomodation a WHERE earth_circle_distance(?0, a.geographical_position) < ?1", nativeQuery = true)
    Accomodation findByRange(Point location, int range);

}

Basically I have replaced the @fiumicino with the ?0 parameter and the 7 value of the distance range with ?1 . 基本上,我已经将@fiumicino替换为?0参数,并将距离范围的7值替换为?1

Then I have created this simple unit test method: 然后,我创建了这个简单的单元测试方法:

@Test
public void findByRangeTest() throws ParseException {

    System.out.println("findByRangeTest() START");

    Accomodation accomodation = null;

    int range = 7;

    double lat = 41.768835;
    double lon = 12.247658;

    Point location = GisUtils.buildGeometry(lat, lon);

    accomodation = accomodationDAO.findByRange(location, range);

    assert(accomodation != null);

    System.out.println("findByRangeTest() END");

}

The problem is that when this method perform the findByRange() method I obtain the following exception into my stacktrace: 问题是,当此方法执行findByRange()方法时,我在我的stacktrace中获取了以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [2] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [2] 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:491)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    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)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy104.findByRange(Unknown Source)
    at com.betrivius.test.dao.DAOTest.findByRangeTest(DAOTest.java:171)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
Caused by: java.lang.IllegalArgumentException: Parameter with that position [2] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:518)
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:674)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:198)
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372)
    at com.sun.proxy.$Proxy107.setParameter(Unknown Source)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:140)
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:100)
    at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:69)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:160)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:151)
    at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateQuery(AbstractStringBasedJpaQuery.java:81)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:188)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:210)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:114)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:104)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 36 more

So, what exactly means this excetpion? 那么,这种兴奋到底意味着什么呢? Why it referer to a parameter with position [2] if in my query I only have 2 params ?0 , and ?1 ? 如果在查询中我只有2个参数?0?1 ,为什么它引用位置为[2]的参数?

What is wrong? 怎么了? What am I missing? 我想念什么? How can I solve this issue? 我该如何解决这个问题?

In bind statements indexes start with 1. 在绑定语句中,索引以1开头。

This should work: 这应该工作:

@Repository
public interface AccomodationDAO extends JpaRepository<Accomodation, Long> {

@Query(value = "SELECT * FROM accomodation a WHERE earth_circle_distance(?1, a.geographical_position) < ?2", nativeQuery = true)
Accomodation findByRange(Point location, int range);

}

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “java.lang.IllegalArgumentException:具有该位置 [1] 的参数不存在”当我使用 spring-data-jpa - "java.lang.IllegalArgumentException: Parameter with that position [1] did not exist" When I use spring-data-jpa Spring JPA-该位置为[1]的参数不存在 - Spring JPA - Parameter with that position [1] did not exist 该位置[1]的参数不存在; 嵌套异常是java.lang.IllegalArgumentException:具有该位置[1]的参数不存在 - Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist Spring:具有该位置的参数不存在异常 - Spring : Parameter with that position did not exist exception 在本机 SQL 查询中使用命名参数和 Spring 数据 JPA - Using Named Parameter in native SQL query with Spring Data JPA 将本机查询移动到 NamedNativeQuery 后,该位置 [1] 的参数不存在? - Parameter with that position [1] did not exist after moving the native query into a NamedNativeQuery? JPA java.lang.IllegalArgumentException:名称为[xxx]的参数不存在 - JPA java.lang.IllegalArgumentException: Parameter with that name [xxx] did not exist 将SQL本机查询转换为Spring数据JPA - Convert SQL native query to Spring data JPA spring 数据 jpa,本机查询未设置查询参数 - spring data jpa, native query not setting query parameter Spring Data JPA 本机 SQL 查询 DELETE SQL 查询 - Spring Data JPA Native SQL Query DELETE SQL Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM