ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Data Solr创建动态域报错:org.springframework.data.solr.UncategorizedSolrException

2019-07-15 10:51:33  阅读:190  来源: 互联网

标签:java data springframework 报错 org SolrTemplate solr


今天在项目中使用Spring Data Solr导入动态域数据报错, 控制台打印错误信息如下

Exception in thread "main" org.springframework.data.solr.UncategorizedSolrException: nested exception is java.lang.NullPointerException
	at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:145)
	at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:199)
	at org.springframework.data.solr.core.SolrTemplate.saveBeans(SolrTemplate.java:194)
	at com.pinyougou.solrutil.ImportItem.importAllItem(ImportItem.java:46)
	at com.pinyougou.solrutil.ImportItem.main(ImportItem.java:53)
Caused by: java.lang.NullPointerException
	at org.springframework.data.solr.core.convert.MappingSolrConverter.writeWildcardMapPropertyToTarget(MappingSolrConverter.java:310)
	at org.springframework.data.solr.core.convert.MappingSolrConverter.access$100(MappingSolrConverter.java:62)
	at org.springframework.data.solr.core.convert.MappingSolrConverter$2.doWithPersistentProperty(MappingSolrConverter.java:287)
	at org.springframework.data.solr.core.convert.MappingSolrConverter$2.doWithPersistentProperty(MappingSolrConverter.java:269)
	at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:311)
	at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java:269)
	at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java:258)
	at org.springframework.data.solr.core.convert.MappingSolrConverter.write(MappingSolrConverter.java:62)
	at org.springframework.data.solr.core.SolrTemplate.convertBeanToSolrInputDocument(SolrTemplate.java:463)
	at org.springframework.data.solr.core.SolrTemplate.convertBeansToSolrInputDocuments(SolrTemplate.java:546)
	at org.springframework.data.solr.core.SolrTemplate.access$100(SolrTemplate.java:91)
	at org.springframework.data.solr.core.SolrTemplate$5.doInSolr(SolrTemplate.java:202)
	at org.springframework.data.solr.core.SolrTemplate$5.doInSolr(SolrTemplate.java:199)
	at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:141)

  

public class TbItem implements Serializable {
    @Field
    private Long id;
   
    //其他属性略
    @Dynamic //动态域
    @Field("item_spec_*")
    private Map specMap;
    
//set() get()略
!
@Component("importItem")
public class ImportItem {
    @Autowired
    private SolrTemplate solrTemplate;
    @Autowired
    private TbItemMapper itemMapper;
 
    private void importAllItem(){
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("1");//只查询状态为 1 的数据
        List<TbItem> list = itemMapper.selectByExample(example);
        for (TbItem item : list) {
            /*
            spec是POJO中另外的属性
            item.getSpec() 得到数据结构: {"网络":"移动4G","机身内存":"32G"}
            使用fastjson 转换为Map,存放在POJO中
             */
            Map specMap = JSON.parseObject(item.getSpec(), Map.class);
            item.setSpecMap(specMap);
        }
        /*
         *统一保存
         */
        solrTemplate.saveBeans(list);
        solrTemplate.commit();
    }
 
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
        ImportItem util = (ImportItem) applicationContext.getBean("importItem");
        util.importAllItem();
    }
}

在网上没有找到对应的解决方案,不断尝试,将POJO中Map的范型加上后结果OK!!!

@Dynamic //动态域
@Field("item_spec_*")
private Map<String,String> specMap;//最好将范型加上
//set() get()略

因为是Maven分模块开发,将POJO重新install就可以了

2019-07-1510:47:32

作者:深海收破烂

标签:java,data,springframework,报错,org,SolrTemplate,solr
来源: https://www.cnblogs.com/itboxue/p/11187550.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有