简体   繁体   中英

Combining Spring-Data for MongoDB and ElasticSearch

I'm trying

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

but that gives me:

 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! ->

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.

The GoalRepository is:

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> {    
 }

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

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.

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, Mark

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM