2017-05-15 99 views
0

我已經安裝使用spring-data-neo4j v 4.2.1,neo4j-ogm v 2.1.2。
我需要使用特定配置的neo4j嵌入式服務器進行測試。 cypher.forbid_shortestpath_common_nodes=false如何配置spring-data-neo4j嵌入式服務器屬性?

我想這一點沒有春天@Configuration豆成功:

@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; 
} 

請,我怎麼設置它的Spring Java配置中?

回答

2

cypher.forbid_shortestpath_common_nodes是Neo4j設置,而不是SDN/OGM,因此您需要在創建數據庫時將其提供給數據庫。

理想的嵌入式數據庫的配置將類似於此:

@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()); 
    } 
} 

然而這不會對SDN 4.2.x版工作,但有一個解決辦法:

@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()); 
    } 
+0

爲什麼你是否在驅動程序中設置了:'Components.setDriver(driver);'當SessionFactory的構造函數調用'Components.configure(configuration);'時調用'Components.destroy();'調用'driver = null; '? – lOlive

+0

第二條評論:SessionFactory.openSession()調用調用Component.loadDriver()調用Component.setDriver(DriverService.load(configuration.driverConfiguration()))的Components.driver()。因此,最終只有您的配置的DriverConfiguration用於創建新的驅動程序。 – lOlive

+0

我們的解決方案是使用SessionFactory的其他構造函數(沒有配置參數的構造函數)。 – lOlive