结合 Spring-Data for MongoDB 和 ElasticSearch [英] Combining Spring-Data for MongoDB and ElasticSearch

查看:48
本文介绍了结合 Spring-Data for MongoDB 和 ElasticSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力

@org.springframework.data.mongodb.core.mapping.Document(collection = "goal")
@org.springframework.data.elasticsearch.annotations.Document(indexName = "goal")
public class Goal implements Serializable {
 ....}

但这给了我:

 Error creating bean with name 'goalRepository':
 Invocation of init method failed; nested exception is
 org.springframework.data.mapping.PropertyReferenceException:
 No property insert found for type Goal! ->

顺便说一句:只要我将名为插入"的属性添加到目标或从目标中删除 elasticsearch 注释,该错误就会消失.

BTW: That error disappears as soon as I add an property with the name 'insert' to Goal or I remove the elasticsearch annotation from goal.

目标存储库是:

package org.jhipster.mongo.repository;
import org.jhipster.mongo.domain.Goal;
import org.springframework.data.mongodb.repository.MongoRepository;

 public interface GoalRepository extends MongoRepository<Goal,String> {    
 }

推荐答案

可以在一个项目中使用多个 Spring Data 模块,但需要注意设置.

Using multiple Spring Data modules in one project is possible but requires attention concerning setting things up.

在类路径上拥有多个 Spring Data 模块可以实现严格的配置,这是 Spring Data 区分存储库职责所必需的.这主要通过注释以及特定存储库是否适合类型层次结构来完成.在您的情况下,Goal 使用 MongoDB 和 Elasticsearch 注释进行了注释,因此这两个模块都渴望实现存储库.

Having more than one Spring Data module on the class path enables strict configuration which is required for Spring Data to distinguish between repository responsibility. This is mainly done by annotations and whether a particular repository fits within the type hierarchy. In your case, Goal is annotated with MongoDB and Elasticsearch annotations, so both modules feel the urge to implement repositories.

目前唯一的方法是将存储库保存在不同的包中,并将这些包用作 @Enable...Repositories 中的基础包.假设您的 Elasticsearch 存储库位于 org.jhipster.elasticsearch.repository 中,您的应用程序配置可能如下所示:

The only way so far is to keep repositories in different packages and to use these packages as base packages in @Enable…Repositories. Assuming your Elasticsearch repos are located in org.jhipster.elasticsearch.repository your application config might look like:

@EnableMongoRepositories("org.jhipster.mongo.repository")
@EnableElasticsearchRepositories("org.jhipster.elasticsearch.repository")
@SpringBootApplication
public class SpringBootApplication { … }

HTH,马克

这篇关于结合 Spring-Data for MongoDB 和 ElasticSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆