Modifying native query cannot have named parameter bindings?

2.2k views Asked by At

I have specified the following modifying native query within my JpaRepository.

public interface PlayerStatisticsRepository extends JpaRepository<PlayerStatistics, PlayerStatisticsKey> {

@Modifying
@Query(value = "INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (:enteredTheFieldAtInSeconds, :leftTheFieldAtInSeconds, :#{#match.primaryKey.homeTeam.name}, :#{#match.primaryKey.matchday.matchdayNumber}, :playedFromTheFirstWhistleblow, :#{#player.id})", nativeQuery = true)
void insertIntoPlayerStatistics(int enteredTheFieldAtInSeconds, int leftTheFieldAtInSeconds,
        boolean playedFromTheFirstWhistleblow, Match match, Player player);
}

Unfortunately it ends with an exception at the runtime saying that supposedly the binding for the match parameter is not there, while it obviously is.

org.springframework.dao.InvalidDataAccessApiUsageException: No parameter binding found for name match!; nested exception is java.lang.IllegalArgumentException: No parameter binding found for name match!
    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.$Proxy185.insertIntoPlayerStatistics(Unknown Source)
    at web_xtra_klasa.SuspensionFromThePreviousRoundTest.ifPlayerHas2YellowCardsFromThePreviousRoundAndReceivedAnotherTwoThenShouldBeSuspendedOnTheNextMatchday(SuspensionFromThePreviousRoundTest.java:425)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: No parameter binding found for name match!
    at org.springframework.data.jpa.repository.query.StringQuery.getBindingFor(StringQuery.java:118)
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.getBindingFor(StringQueryParameterBinder.java:75)
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:60)
    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:190)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:242)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:82)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
    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:282)
    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)
    ... 38 more

In one of the comments here, someone is writing that SpEL does not work if there is more than one parameter but I have also another select query that is not native and it does have more than one parameter and works flawlessly.

Indexed access works like a charm.

@Modifying
@Query(value = "INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (?#{[0]}, ?#{[1]}, ?#{[3].primaryKey.homeTeam.name}, ?#{[3].primaryKey.matchday.matchdayNumber}, ?#{[2]}, ?#{[4].id})", nativeQuery = true)
void insertIntoPlayerStatistics(int enteredTheFieldAtInSeconds, int leftTheFieldAtInSeconds,
        boolean playedFromTheFirstWhistleblow, Match match, Player player);

Has anybody experienced a similar issue? Is this a bug? I could not find any open issue on that.

2

There are 2 answers

1
stiebrs On BEST ANSWER

I might be a bit late for this particular party, but bumped into same issue. Turns out you shouldn't mix ways of passing parameters - if you use SpEL, be consistent and use it everywhere. E.g.

INSERT INTO x (x,y,z) VALUES (:x, :#{#y}, ?[0])

will not work, but

INSERT INTO x (x,y,z) VALUES (:#{#x}, :#{#y}, :#{#[4]})

runs just fine.

Edit: you obviously noticed, that your second example works as well, since you consistently use SpEL there.

0
Fayez Zalloum On

You need to order the parameters exactly as they appear in your native Query, so the query become:

@Query(value = "INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (:enteredTheFieldAtInSeconds, :leftTheFieldAtInSeconds, :#{#match.primaryKey.homeTeam.name}, :#{#match.primaryKey.matchday.matchdayNumber}, :playedFromTheFirstWhistleblow, :#{#player.id})", nativeQuery = true)

void insertIntoPlayerStatistics(int enteredTheFieldAtInSeconds, int leftTheFieldAtInSeconds,
   Match match, boolean playedFromTheFirstWhistleblow, Player player);