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());
}
爲什麼你是否在驅動程序中設置了:'Components.setDriver(driver);'當SessionFactory的構造函數調用'Components.configure(configuration);'時調用'Components.destroy();'調用'driver = null; '? – lOlive
第二條評論:SessionFactory.openSession()調用調用Component.loadDriver()調用Component.setDriver(DriverService.load(configuration.driverConfiguration()))的Components.driver()。因此,最終只有您的配置的DriverConfiguration用於創建新的驅動程序。 – lOlive
我們的解決方案是使用SessionFactory的其他構造函數(沒有配置參數的構造函數)。 – lOlive