How to configure spring-data-neo4j embedded server properties?

笑着哭i 提交于 2019-12-12 04:48:50

问题


I have setup using spring-data-neo4j v 4.2.1, neo4j-ogm v 2.1.2.
I need embedded neo4j server with specific configuration for testing. cypher.forbid_shortestpath_common_nodes=false.

I tried this without success in spring @Configuration bean:

@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
    org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
    config.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
    config.set("cypher.forbid_shortestpath_common_nodes", false);
    return config;
}

Please, how do I set it up within spring java configuration?


回答1:


The cypher.forbid_shortestpath_common_nodes is Neo4j setting, not SDN/OGM so you need to provide this to the database when you create it.

Ideally the configuration for an embedded database would look similar to this:

@Configuration
@EnableNeo4jRepositories(basePackageClasses = UserRepository.class)
@ComponentScan(basePackageClasses = UserService.class)
static class EmbeddedConfig {

    @Bean(destroyMethod = "shutdown")
    public GraphDatabaseService graphDatabaseService() {
        GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
            .newEmbeddedDatabaseBuilder(new File("target/graph.db"))
            .setConfig(GraphDatabaseSettings.forbid_shortestpath_common_nodes, "false")
            .newGraphDatabase();

        return graphDatabaseService;
    }

    @Bean
    public SessionFactory getSessionFactory() {
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
        EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
        Components.setDriver(driver);
        return new SessionFactory(configuration, User.class.getPackage().getName());
    }

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception {
        return new Neo4jTransactionManager(getSessionFactory());
    }
}

However this does not work for SDN 4.2.x, but there is a workaround:

    @Bean
    public SessionFactory getSessionFactory() {
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
        // Register your configuration here, this will confuse OGM so the driver you set below won't be destroyed
        Components.configure(configuration);

        // Register your driver
        EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
        Components.setDriver(driver);

        // Set driver class name so you won't get NPE
        configuration.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");

        return new SessionFactory(configuration, User.class.getPackage().getName());
    }



回答2:


As of neo4j-ogm-embedded-driver version 3.2.1 the following approach can be applied.

@Configuration
public class EmbeddedNeo4jConfig {

    @Bean
    org.neo4j.ogm.config.Configuration getConfiguration() {

        var builder = new org.neo4j.ogm.config.Configuration.Builder()
                .neo4jConfLocation("neo4j.conf");
        return builder.build();
    }
}

test/resources/neo4j.conf:

cypher.forbid_shortestpath_common_nodes=false


来源:https://stackoverflow.com/questions/43974669/how-to-configure-spring-data-neo4j-embedded-server-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!