Reputation: 6604
I want to check if a document with 15 equal fields alredy exists in a collection. And it seems that the @Query
annotation can not handle more than 10 parameters. The following class throws an exception on calling is duplicated.
public interface ReportingentriesRepository extends MongoRepository<Reportingentries, String> {
@Query(exists = true,value = "{ 'foo': ?0,'foo1': ?1,'foo2': ?2,'foo3': ?3,'foo4': ?4,'foo5': ?5,'foo6': ?6,'foo7': ?7,'foo8': ?8,'foo9': ?9, 'foo10': ?10}")
boolean isDuplicated(String value0, String value1, String value2, String value3, String value4, String value5, String value6, String value7, String value8, String value9, String value10);
}
The exception is:
java.util.NoSuchElementException: Could not to find binding for placeholder '?11'.
at org.springframework.data.mongodb.repository.query.ExpressionEvaluatingParameterBinder$BindingContext.getBindingFor(ExpressionEvaluatingParameterBinder.java:341) ~[spring-data-mongodb-1.10.0.RELEASE.jar:?]
at org.springframework.data.mongodb.repository.query.ExpressionEvaluatingParameterBinder.replacePlaceholders(ExpressionEvaluatingParameterBinder.java:120) ~[spring-data-mongodb-1.10.0.RELEASE.jar:?]
at org.springframework.data.mongodb.repository.query.ExpressionEvaluatingParameterBinder.bind(ExpressionEvaluatingParameterBinder.java:91) ~[spring-data-mongodb-1.10.0.RELEASE.jar:?]
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.createQuery(StringBasedMongoQuery.java:130) ~[spring-data-mongodb-1.10.0.RELEASE.jar:?]
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:84) ~[spring-data-mongodb-1.10.0.RELEASE.jar:?]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483) ~[spring-data-commons-1.13.0.RELEASE.jar:?]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.0.RELEASE.jar:?]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.13.0.RELEASE.jar:?]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.0.RELEASE.jar:?]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE]
at com.sun.proxy.$Proxy94.isDuplicated(Unknown Source) ~[?:?]
at com.company.importer.Application.run(Application.java:29) [classes/:?]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE]
at com.company.importer.Application.main(Application.java:22) [classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_92]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.1.RELEASE.jar:1.5.1.RELEASE]
Am I missing something or does spring simply not support this?
I'm using spring-boot-starter-data-mongodb:1.5.1.RELEASE
Upvotes: 4
Views: 1992
Reputation: 540
I don't know if you already solved this issue, but I was having two different issues, you can check here: how-to-use-the-same-parameter-for-multiple-conditions
The first issue is that I want to repeat a parameter, so I tried with @Param
then I started to got the same error as you which I couldn't use more than 10 parameters.
So I decide to debugging the binding processes and I solve both problems adding a simple single quote to the parameters, like this (check the parameter number 6):
@Query(value = "{ companyType: ?0, companyId: ?1, eventType: ?2, " +
"delivered: ?3, createdAt: { $gt: ?4, $lt: ?5 }, " +
"$or: [{ $or: [{ $or: [{ $or: [{ requestPayload.sponsorGovernmentId: ?6 }, " +
"{requestPayload.buyerGovernmentId: '?6'}] }, {requestPayload.supplierGovernmentId: '?6'}] }, " +
"{requestPayload.invoiceNumber: ?7}]}, {requestPayload.externalId: ?8}] }")
After that I'm able to use more than 10 parameters and repeat it.
Upvotes: 0
Reputation: 1424
Spring data has one more annotation which would help to treat the method parameter as query parameter.
Annotation: @Param
This annotation can be used with the method parameter itself. Please find below format which can help to extend the method parameter count to use with spring data.
Explaination: You can include N no. of method parameter with @Param
annotation. Please find below valid expression to write annotation.
@Param("param0")
Now, when you add this @Param annotation with method parameter, you must need to specify parameter in @Query
with the prefix of colon (:)
. Something like param0 in below query,
@Query("{"query": {"bool": {"must": [{ "match": {"userId": ":param0" }}]}}}")
Complete Example: You can see org_id is the query parameter.
@Query("{"query": {"bool": {"must": [{ "match": {"userId": ":org_Id"}}]}}}")
List<User> getByOrgId(@Param("org_Id") String orgId)
Upvotes: 1