java - 结合 Spring-Data for MongoDB 和 ElasticSearch

标签 java spring mongodb spring-data-mongodb spring-data-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! ->

顺便说一句:当我向 Goal 添加名为“insert”的属性或从目标中删除 elasticsearch 注释时,该错误就会消失。

目标存储库是:

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 模块是可能的,但需要注意设置。

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

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

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

马克·HTH

关于java - 结合 Spring-Data for MongoDB 和 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36376447/

相关文章:

java - 正确设置 Eclipse 插件项目

java - 产生多个结果的键

java - JSON Java 将空数组添加到 JSON 对象

java - OSGI 容器未加载 postgresql 驱动程序?

node.js - 如何在node.js中隐藏 Mongoose 连接信息?

javascript - Mongoose 验证给出 "CastError: Cast to undefined failed for value"

mongodb - 更新包含在 MongoDB 文档中的数组中的子文档

java - 如何使用 Maven 部署多项目?

java - Spring mvc 将 url "myapp.com/Foo/12345/test-one"重写为 "myapp.com/Foo/12345/test-one-a-b"

java - spring boot如何在目标中创建jar文件而无需在pom中添加插件