jmdev jmdev - 1 year ago 117
Java Question

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{

private MyNeo4jBasedRepository myNeo4jBasedRepository;

private MyH2BasedRepository myH2BasedRepoistory;

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

Caused by:
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
[] is defined:
expected single matching bean but found 2:

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!

Answer Source

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:

public void setMappingContext(TODO todo)
  //super.setMappingContext(todo) Sample implementation as before

This will ensure Spring selects the correct dependency.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download