我已经使用Spring-Data-Neo4J V 4.2.1,Neo4J-OGM v 2.1.2进行了设置。
我需要具有特定配置的嵌入式neo4J服务器进行测试。 cypher.forbid_shortestpath_common_nodes=false.
我在春季@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;
}
请,如何在Spring Java配置中进行设置?
看答案
这 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());
}
作者:Jeebiz 创建时间:2023-12-28 12:59
最后编辑:Jeebiz 更新时间:2023-12-28 13:00
最后编辑:Jeebiz 更新时间:2023-12-28 13:00