Using spring-data-mongodb and spring-data-neo4j together

632 views Asked by At

How can I use spring-data-mongodb and spring-data-neo4j in the same spring-boot application?

I can easily use one or the other following the "getting started" guides, but as soon as I try to add Neo4J to a MongoDB application then I get runtime errors such as:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'application': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type MongoBook!

I've setup a minimal example at https://github.com/afaulconbridge/myspring-mongo-neo

3

There are 3 answers

0
afaulconbridge On BEST ANSWER

As @manish pointed out, you need to make Spring Data MongoDB and Spring Data Neo4J scan separate packages. i.e.

@EnableMongoRepositories(basePackageClasses=MongoBook.class)
@EnableNeo4jRepositories(basePackageClasses=NeoAuthor.class)

I've updated the example project at https://github.com/afaulconbridge/myspring-mongo-neo with a solution.

0
digx1 On

You can try this project which uses JPA and Neo4J together. The structure should technically work with Mongo as well. Be aware though that Mongo doesn't support the concept of transactions so you may not have to define an explicit transaction manager for each Spring Data project.

0
Boris Treukhov On

You should be able to use excludeFilters and includeFilters parameters respectively even in the same package (in most cases includeFilters is enough)

@EnableMongoRepositories(basePackageClasses=MongoBook.class,
  includeFilters ={@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
  classes = {MongoRepository.class}))

@EnableNeo4jRepositories(basePackageClasses=NeoAuthor.class,
  includeFilters ={@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, 
  classes = {NeoRepository.class}))

from includeFilters() description

Specifies which types are eligible for component scanning. Further narrows the set of candidate components from everything in {#basePackages()} to everything in the base packages that matches the given filter or filters.