Invalid parameter index! You seem to have declare too little query method parameters! - Spring Mongo TextSearch

依然范特西╮ 提交于 2019-12-08 12:48:07

问题


I am developing Spring + Mongo Data JPA example for Text Search. When I am trying to search using OrderID 102. I was expecting all records to be fetch from DB those record contains 102 in OrderID. Please help me I see following error is coming.

org.springframework.data.repository.query.ParameterOutOfBoundsException: Invalid parameter index! You seem to have declare too little query method parameters!
    at org.springframework.data.repository.query.Parameters.getParameter(Parameters.java:178)
    at org.springframework.data.repository.query.Parameters.getBindableParameter(Parameters.java:248)
    at org.springframework.data.repository.query.ParametersParameterAccessor.getBindableValue(ParametersParameterAccessor.java:107)
    at org.springframework.data.repository.query.ParametersParameterAccessor$BindableParameterIterator.next(ParametersParameterAccessor.java:165)
    at org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor$ConvertingIterator.next(ConvertingParameterAccessor.java:176)
    at org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor$ConvertingIterator.nextConverted(ConvertingParameterAccessor.java:185)
    at org.springframework.data.mongodb.repository.query.MongoQueryCreator.from(MongoQueryCreator.java:244)
    at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:114)
    at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:58)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
    at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.createQuery(PartTreeMongoQuery.java:76)
    at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:85)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:462)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
    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.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:207)
    at com.sun.proxy.$Proxy22.findByOrderId(Unknown Source)
    at com.common.index.OrderTest.testOrderSearch(OrderTest.java:29)
    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:70)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    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:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    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:675)
    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.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at org.springframework.data.repository.query.Parameters.getParameter(Parameters.java:176)
    ... 52 more

The code I developed: Order.java

@Document(collection="orders")
public class Order {

    @Id
    private ObjectId id;

    @Field("OrderID")
    private Integer orderId;

    @Field("CustomerID")
    private String customerId;

    @Field("EmployeeID")
    private Integer employeeId;

    @Field("OrderDate")
    private String orderDate;

    @Field("RequiredDate")
    private String requiredDate;

    @Field("ShippedDate")
    private String shippedDate;

    @Field("ShipVia")
    private Integer shipVia;

    @Field("Freight")
    private Double freight;

    @Field("ShipName")
    private String shipName;

    @Field("ShipAddress")
    private Integer shipAddress;

    @Field("ShipCity")
    private String shipCity;

    @Field("ShipRegion")
    private String shipRegion;

    @Field("ShipPostalCode")
    private String shipPostalCode;

    @Field("ShipCountry")
    private Integer shipCountry;

    @Field("field14")
    private String field14;

        // setters and getters
}

OrderRepository.java

public interface OrderRepository extends CrudRepository<Order, Integer>{
    List<Order> findByOrderId(TextCriteria criteria);
}

This is to create Index in mongoDB:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-config.xml"})
public class IndexToolTest {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void testCreateIndex() {
        mongoTemplate.indexOps(Order.class).ensureIndex(new TextIndexDefinitionBuilder()
        .onField("OrderID").build());
    }
}

OrderTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-config.xml"})
public class OrderTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderTest.class);

    @Autowired
    private OrderRepository orderRepository;

    @Test
    public void testOrderSearch() {
        TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("102");
        List<Order> orders = orderRepository.findByOrderId(criteria);
        System.out.println("Size : "+orders.size());
    }
}

application-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:mongo="http://www.springframework.org/schema/data/mongo"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

    <context:annotation-config/>
    <mvc:annotation-driven/>

    <beans:bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="locations">
            <beans:list>
                <beans:value>classpath:database.properties</beans:value>
             </beans:list>
        </beans:property>
    </beans:bean>

    <context:component-scan base-package="com.common.model"/>

    <!-- Default bean name is 'mongo' -->
    <mongo:mongo id="mongo" host="${mongo.host}" port="${mongo.port}"/>

    <mongo:repositories base-package="com.common.repository"/>

    <beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <beans:constructor-arg ref="mongo"/>
        <beans:constructor-arg name="databaseName" value="${mongo.database.name}"/>
    </beans:bean>

    <beans:bean id="orderService" class="com.common.Service.OrderServiceImpl" />

</beans:beans>

I've provided all sufficient information. Please help now.


回答1:


The method List<Order> findByOrderId(TextCriteria criteria); is missing the required argument for orderId which is present in both the domain type and the query method declaration. The TextCriteria argument is a special one that will not be mapped to any of the arguments of the method declaration.

Remove OrderBy from your query to just use the criteria.

List<Order> findAllBy(TextCriteria criteria);


来源:https://stackoverflow.com/questions/36185175/invalid-parameter-index-you-seem-to-have-declare-too-little-query-method-parame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!