jmdev
jmdev

Reputation: 181

Enabling two Spring Data repositories (neo4j and h2) in the same application context

I can't seem to get SDN (latest snapshot) to play nicely along side Spring Data JPA (H2). I do not need transacational support across both data stores; instead my desire is to simply utilize repositories for both stores in the same class. For example

public MySpringControlledClass{

   @Autowired
   private MyNeo4jBasedRepository myNeo4jBasedRepository;

   @Autowired
   private MyH2BasedRepository myH2BasedRepoistory;
   ...
}

When I enable both neo4j and JPA I get an exception of the form

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myNeo4jBasedRepository': Unsatisfied dependency expressed through method 'setMappingContext' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.mapping.context.MappingContext] is defined: expected single matching bean but found 2: neo4jMappingContext,jpaMappingContext

Which is as expected given that I now have two mapping contexts, one implicitly created by SDN and one explicitly created in my configuration for spring data JPA.

While I have found posts describing how to do this with two different databases in spring data jpa, I haven't found an example of how to do it with SDN and a spring data JPA store such as H2. The difference seems to be that SDN hides some of the boilderplate logic from the developer.

Any help would be much appreciated as I have spent a nice chunk of time trying various things and none have proven fruitful thus far.

Thanks so much!

Upvotes: 3

Views: 1595

Answers (2)

digx1
digx1

Reputation: 1088

There has been a bug fix pushed to Spring Data Neo4j 4.2.0.BUILD-SNAPSHOT which means you don't need to use the @Qualifieranymore or define a MappingContext bean in your configuration. You will only need to define the respective PlatformTransactionManagers and link them to your @EnableXXXRepositories through the transactionManagerRef attribute.

I have created a project here to demonstrate how to get the two Spring Data projects working together with Spring Boot: https://github.com/mangrish/spring-data-jpa-neo4j-with-boot.

Upvotes: 2

Derrops
Derrops

Reputation: 8137

So in your myNeo4jBasedRepository there is a setMappingContext method which is auto-wired, and it doesn't know whether to use neo4jMappingContext or jpaMappingContext because both those beans have the same type as the dependency.

I'm not sure how much is exposed to you but if possible in your MyNeo4jBasedRepository override your setMappingContext method to take a type of whatever the neo4jMappingContext bean's type is to get it to select this one.

Or override setMappingContext method as simply super.setMappingContext and put a qualifier @Qualifier("neo4jMappingContext") at the top:

@Autowired
@Qualifier("neo4jMappingContext")
public void setMappingContext(TODO todo)
{
  //super.setMappingContext(todo) Sample implementation as before
}

This will ensure Spring selects the correct dependency.

Upvotes: 3

Related Questions