使用 Spring-Data-Solr 将数据索引到 Solr 时未发现关联错误
No Association Found Error while indexing data to Solr using Spring-Data-Solr
我正在尝试 spring 数据 mongoDB + spring 数据 solr 的示例服务应用程序,其中 MongoDB 用于保存数据和 solr 用于索引和正在搜索。
对 MongoDB 的保存操作在服务 class 中成功发生。但是在调用 SolrOperation
save()
方法时服务崩溃,错误日志如下:
SEVERE [com.sun.jersey.spi.container.ContainerResponse] (defaulttask-1)The
RuntimeException could not be mapped to a response, re-throwing the HTTP
container:org.springframework.data.solr.UncategorizedSolrException:No
association fond!; nested exception is java.lang.IllegalStateException: No
association found! at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:171)
当我进一步深入分析日志时,它说:
Caused by: java.lang.IllegalStateException:No association found!
at org.springframework.data.mapping.PersistentProperty.getRequiredAssociation(PersistentProperty.java:166)
SolrTemplate
内 convertBeanToSolrInputDocument ()
内的 getConverter().write(bean, document)
行抛出错误。
DAO 方法
public String addToRepo(MyEntity myEntity){
mongoOperation.save(myEntity); //works fine data saved to MongoDB
solrOperation.save("collectionName",myEntity); //generates above exception
return "success";
}
我正在使用 Spring 5 + solrj-6.1.0 + spring-data-solr-4.0.2.
solroperation
已正确加载为:
ApplicationContext SOLR_CONFIG_APP_CTX = new AnnotationConfigApplicationContext(SpringSolrConfig.class);
SolrOperations solrOperation = (SolrOperations)ctx.getBean("solrTemplate");
public static final SolrOperations SOLR_OPS=
(SolrOperations)SOLR_CONFIG_APP_CTX.getBean("solrTemplate");
SpringSolrConfig.java
@Configuration
public class SpringSolrConfig extends AbstractSolrConfig {
public SolrClientFactory solrClientFactory (){
SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
HttpSolrClientFactory solrClientFactory = new HttpSolrClientFactory (solrClient);
return solrClientFactory;
}
}
SpringConfig.xml 文件如下所示:
<mongo:mongo host="195.168.1.140" port="27017"/>
<mongo:dbfactory dbname="myDB"/>
<bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg-name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<repositories base-package="sample.package.repositories"/>
<bean id="myEntityRepo" class="sample.package..repositories.MyEntityRepositoryInterface"/>
<solr:repositories base-package="sample.package.repositories"/>
<solr:sorl-server id="solrServer" url="http://localhost:8983/solr"/>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg index="0" ref="solrServer"/>
</bean>
提前感谢您帮助我解决此问题!
我更新了我的 SpringSolrConfig 文件来解决这个问题。礼貌:https://jira.spring.io/browse/DATASOLR-394
@Configuration
public class SpringSolrConfig extends AbstractSolrConfig {
String solrUrl = "http://localhost:8983/solr/"; // TODO read this ideally from spring-configuration.xml file
public SolrClientFactory solrClientFactory (){
SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
HttpSolrClientFactory solrClientFactory = new HttpSolrClientFactory (solrClient);
return solrClientFactory;
}
@Bean
public SolrTemplate solrTemplate () {
SolrTemplate solrTemplateObj = new SolrTemplate(solrClientFactory));
// This ensures that the default MappingSolrConverter.java is not used for converting the bean to a Solr Document before indexing
solrTemplateObj.setSolrConverter(new SolrJConverter());
return solrTemplateObj;
}
}
我正在尝试 spring 数据 mongoDB + spring 数据 solr 的示例服务应用程序,其中 MongoDB 用于保存数据和 solr 用于索引和正在搜索。
对 MongoDB 的保存操作在服务 class 中成功发生。但是在调用 SolrOperation
save()
方法时服务崩溃,错误日志如下:
SEVERE [com.sun.jersey.spi.container.ContainerResponse] (defaulttask-1)The
RuntimeException could not be mapped to a response, re-throwing the HTTP
container:org.springframework.data.solr.UncategorizedSolrException:No
association fond!; nested exception is java.lang.IllegalStateException: No
association found! at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:171)
当我进一步深入分析日志时,它说:
Caused by: java.lang.IllegalStateException:No association found!
at org.springframework.data.mapping.PersistentProperty.getRequiredAssociation(PersistentProperty.java:166)
SolrTemplate
内 convertBeanToSolrInputDocument ()
内的 getConverter().write(bean, document)
行抛出错误。
DAO 方法
public String addToRepo(MyEntity myEntity){
mongoOperation.save(myEntity); //works fine data saved to MongoDB
solrOperation.save("collectionName",myEntity); //generates above exception
return "success";
}
我正在使用 Spring 5 + solrj-6.1.0 + spring-data-solr-4.0.2.
solroperation
已正确加载为:
ApplicationContext SOLR_CONFIG_APP_CTX = new AnnotationConfigApplicationContext(SpringSolrConfig.class);
SolrOperations solrOperation = (SolrOperations)ctx.getBean("solrTemplate");
public static final SolrOperations SOLR_OPS=
(SolrOperations)SOLR_CONFIG_APP_CTX.getBean("solrTemplate");
SpringSolrConfig.java
@Configuration
public class SpringSolrConfig extends AbstractSolrConfig {
public SolrClientFactory solrClientFactory (){
SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
HttpSolrClientFactory solrClientFactory = new HttpSolrClientFactory (solrClient);
return solrClientFactory;
}
}
SpringConfig.xml 文件如下所示:
<mongo:mongo host="195.168.1.140" port="27017"/>
<mongo:dbfactory dbname="myDB"/>
<bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg-name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<repositories base-package="sample.package.repositories"/>
<bean id="myEntityRepo" class="sample.package..repositories.MyEntityRepositoryInterface"/>
<solr:repositories base-package="sample.package.repositories"/>
<solr:sorl-server id="solrServer" url="http://localhost:8983/solr"/>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg index="0" ref="solrServer"/>
</bean>
提前感谢您帮助我解决此问题!
我更新了我的 SpringSolrConfig 文件来解决这个问题。礼貌:https://jira.spring.io/browse/DATASOLR-394
@Configuration
public class SpringSolrConfig extends AbstractSolrConfig {
String solrUrl = "http://localhost:8983/solr/"; // TODO read this ideally from spring-configuration.xml file
public SolrClientFactory solrClientFactory (){
SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
HttpSolrClientFactory solrClientFactory = new HttpSolrClientFactory (solrClient);
return solrClientFactory;
}
@Bean
public SolrTemplate solrTemplate () {
SolrTemplate solrTemplateObj = new SolrTemplate(solrClientFactory));
// This ensures that the default MappingSolrConverter.java is not used for converting the bean to a Solr Document before indexing
solrTemplateObj.setSolrConverter(new SolrJConverter());
return solrTemplateObj;
}
}