Spring data Solr: Spring data Solr: IllegalArgum

今天在使用Spring-data-solr进行Solr数据操作时:遇到错误

Spring data Solr: IllegalArgumentException - this argument is required

网上搜索后找到跟我一样的问题:

http://stackoverflow.com/questions/19893276/spring-data-solr-illegalargumentexception-this-argument-is-required

在作者的启迪下发现Schema.xml果然为multiValued=true
<field name="entity" type="string" indexed="true" stored="true" multiValued="true"/>
跟作者思路不同,因为这个字段必须为多值,后来观察数据格式,将Java中的数据改为数组即String[],问题解决!

下面附上stackover原文:

Spring data Solr: IllegalArgumentException - this argument is required

Whenever I try to query Solr using a Spring data repository I get the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.util.Assert.notNull(Assert.java:123)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:317)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readCollection(MappingSolrConverter.java:423)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:331)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:308)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.getPropertyValue(MappingSolrConverter.java:294)
    at org.springframework.data.solr.core.convert.MappingSolrConverter.getValue(MappingSolrConverter.java:147)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$1.doWithPersistentProperty(MappingSolrConverter.java:134)
    at org.springframework.data.solr.core.convert.MappingSolrConverter$1.doWithPersistentProperty(MappingSolrConverter.java:126)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257)
    at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:126)
    at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:113)
    at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:88)
    at org.springframework.data.solr.core.SolrTemplate.convertSolrDocumentListToBeans(SolrTemplate.java:404)
    at org.springframework.data.solr.core.SolrTemplate.convertQueryResponseToBeans(SolrTemplate.java:396)
    at org.springframework.data.solr.core.SolrTemplate.queryForPage(SolrTemplate.java:276)
    at org.springframework.data.solr.repository.query.AbstractSolrQuery$AbstractQueryExecution.executeFind(AbstractSolrQuery.java:312)
    at org.springframework.data.solr.repository.query.AbstractSolrQuery$CollectionExecution.execute(AbstractSolrQuery.java:335)
    at org.springframework.data.solr.repository.query.AbstractSolrQuery.execute(AbstractSolrQuery.java:129)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:323)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at $Proxy15.findByTitleStartingWith(Unknown Source)
    at foo.bar.Application.main(Application.java:46)

Document:

public class Product {

    @Id
    @Field
    private String id;

    @Field
    private String description;

    @Field
    private String title;

    // getter/setter
} 

Repository:

public interface ProductRepository extends SolrCrudRepository<Product, String> {
    List<Product> findByTitleStartingWith(String title);
}

Configuration + Test:

@ComponentScan
@EnableSolrRepositories("foo.bar.repository")
public class Application {

    @Bean
    public SolrServer solrServer() {
        return new HttpSolrServer("http://localhost:8983/solr");
    }

    @Bean
    public SolrTemplate solrTemplate(SolrServer server) throws Exception {
        return new SolrTemplate(server);
    }    

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        ProductRepository repository = context.getBean(ProductRepository.class);

        Product product = new Product();
        product.setTitle("Foo title");
        product.setDescription("Bar description");
        product.setId(UUID.randomUUID().toString());
        repository.save(product);

        List<Product> list = repository.findByTitleStartingWith("Foo"); // <-- error

        System.out.println("result: " + list);          
    }
}
share improve this question
 

1 Answer

up vote 2 down vote accepted

I Found the solution myself:

I was using the default field configuration for Solr (schema.xml). This configuration includes the fields title and description by default:

<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="description" type="text_general" indexed="true" stored="true"/>

By default title is a multiValued field. This caused an error when Spring data tried to map the fields back to my Product class.

Removing multiValued="true" (or setting it to false) from the title field solved the problem.

share improve this answer
 
 
thank you for providing the answer - will try to improve error messages –   Christoph Strobl  Nov 11 '13 at 6:42

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值