Prakash Balodi
Prakash Balodi

Reputation: 33

How to resolve spring bean conflicts with between spring-data-neo4j and Grails 3.3.6

I have a Grails 3.3.6 application. I wanted to merge some neo4j functionality in this app from a different spring boot application. So I added the following deps from spring boot app -

compile "org.neo4j:neo4j:3.1.0"
compile "org.neo4j.driver:neo4j-java-driver:1.1.0"
compile "org.neo4j:neo4j-ogm-core:2.1.6"
compile "org.neo4j:neo4j-ogm-http-driver:2.1.6"
runtime "org.neo4j:neo4j-ogm-embedded-driver:2.1.6"
runtime "org.neo4j:neo4j-ogm-bolt-driver:2.1.6"
compile "org.springframework.boot:spring-boot-starter-data-neo4j"
compile 'org.springframework.data:spring-data-neo4j:4.2.0.RELEASE'

And put the source files in the src folder of my Grails app. The spring boot app is using a Configuration class to create beans, which looks like this -

Configuration
@EnableNeo4jRepositories("com.server.repositories")
public class TestConfiguration {

@Value("${spring.data.neo4j.uri}")
String neo4jURL;

@Value("${spring.data.neo4j.username}")
String neo4jUser;

@Value("${spring.data.neo4j.password}")
String neo4jPass;

@Bean
public org.neo4j.ogm.config.Configuration configuration() {
    return new org.neo4j.ogm.config.Configuration.Builder().credentials(neo4jUser, neo4jPass).uri(neo4jURL).build();
}

@Bean
public SessionFactory neo4jSessionFactory() {
    // with domain entity base package(s)
    return new SessionFactory(configuration(), "com.server.model");
}

@Bean
public Neo4jTransactionManager neo4jTransactionManager() {
    return new Neo4jTransactionManager(neo4jSessionFactory());
}


@Bean
public Session neo4jSession() {
    return neo4jTransactionManager().getSessionFactory().openSession();
}

When I run the grails app, I get spring bean conflict exceptions. Looks like Grails and neo4j are trying to create some beans with same names -

Unsatisfied dependency expressed through field 'neo4jSession'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.data.neo4j.transaction.SharedSessionCreator#0': Unsatisfied dependency expressed through method 'createSharedSession' parameter 0: Could not convert argument value of type [org.hibernate.internal.SessionFactoryImpl] to required type [org.neo4j.ogm.session.SessionFactory]: Failed to convert value of type 'org.hibernate.internal.SessionFactoryImpl' to required type 'org.neo4j.ogm.session.SessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.hibernate.internal.SessionFactoryImpl' to required type 'org.neo4j.ogm.session.SessionFactory': no matching editors or conversion strategy found

How should I go about resolving these bean conflicts?

Upvotes: 2

Views: 394

Answers (1)

meistermeier
meistermeier

Reputation: 8262

You have to specify the bean name of your Neo4j session factory. In your case this should work as you expected:

@EnableNeo4jRepositories(sessionFactoryRef = "neo4jSessionFactory", basePackages = "com.server.repositories")

Background information: Spring Data Neo4j will look for a bean named sessionFactory. As you can see in your exception Hibernate also creates a bean with such a name and they get in conflict.

Upvotes: 1

Related Questions