2017-05-15 9 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 내장 서버 속성을 구성하는 방법은 무엇입니까?

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

하십시오, 어떻게 내가 봄 자바 구성 내에서 설정합니까 :

나는 봄의 @Configuration 빈의 성공없이 시도?

답변

2

cypher.forbid_shortestpath_common_nodes은 SDN/OGM이 아닌 Neo4j 설정이므로 사용자가 데이터베이스를 만들 때이를 데이터베이스에 제공해야합니다.

@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

이유는 SessionFactory의 생성자가'Components.configure (configuration);을 호출하면'Components.setDriver (driver);로 드라이버를 설정합니까? '? – lOlive

+0

두 번째 주석 :'SessionFactory.openSession()'은 Components.setDriver (DriverService.load (configuration.driverConfiguration()))을 호출하는'Components.loadDriver()'를 호출하는'Components.driver()'를 호출합니다. 따라서 결국 새 드라이버를 만드는 데 Configuration의 DriverConfiguration 만 사용됩니다. – lOlive

+0

우리의 솔루션은 SessionFactory의 다른 생성자 (구성 매개 변수없이)를 사용하는 것입니다. – lOlive