Spring Data Neo4J Database Connection

I am trying to connect to Neo4J database using Spring Data With following link (http://projects.spring.io/spring-data-neo4j/#quick-start). But it doesn't seem to work. Its not able to create Repository Implementation by itself. Thats why I am getting BeanCreationException. Here what I have tried so far.

  • PersistenceContext.java
  • @Configuration
    @EnableTransactionManagement
    @ComponentScan("com.ksh.neo4j")
    @EnableNeo4jRepositories("com.ksh.neo4j.repositories")
    public class PersistenceContext  {
    
      @Bean
      public org.neo4j.ogm.config.Configuration getConfiguration() 
      {
          org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
          configuration.driverConfiguration()
                       .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
                       .setURI("http://localhost:7474");
          return configuration;
      }
      @Bean
      public Neo4jTransactionManager transactionManager() {
          return new Neo4jTransactionManager(getSessionFactory());
      }
      @Bean
      public SessionFactory getSessionFactory() {
          return new SessionFactory("com.ksh.neo4j.domains");
      }
    }
    
  • PersonRepository.java
  • @Repository
    public interface PersonRepository extends GraphRepository<Person> {
    }
    
  • PersonService.java
  • @Service
    public class PersonService {  
      @Autowired
      private PersonRepository personRepository;
    
      public Person save(Person person) {
          return personRepository.save(person);
      }
    }
    
  • PersonController.java
  • @RestController
    @RequestMapping("/person/**")
    public class PersonController {
      @Autowired
      private PersonService personService;
    
      @RequestMapping("/save")
      public ResponseObject save(@RequestBody Person person) 
      {
          try {
              personService.save(person);
              return ResponseObjectCreator.createResponseObj(ResponseStatus.SUCSESS);
          }
          catch (Exception e) {
              e.printStackTrace();
          }
          return ResponseObjectCreator.createResponseObj(ResponseStatus.FAILURE); 
      }
    }
    
  • WebMvcConfig.java
  •  @Configuration
        @EnableWebMvc
        @ComponentScan(basePackages = {"com.ksh.neo4j.controllers", "com.ksh.neo4j"})
        public class WebMvcConfig extends WebMvcConfigurerAdapter {]
          @Override
          public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
              configurer.enable();
          }
        }
    
  • POM.xml
  • <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-commons</artifactId>
      <version>1.13.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-neo4j</artifactId>
      <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-embedded-driver</artifactId>
      <version>2.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-core</artifactId>
      <version>2.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-ogm-http-driver</artifactId>
      <version>2.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>neo4j-kernel</artifactId>
      <version>3.1.3</version>
    </dependency>
    <dependency>
      <groupId>org.neo4j.app</groupId>
      <artifactId>neo4j-server</artifactId>
      <version>3.1.3</version>
    </dependency>
    

    I am getting following exception

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is org.neo4j.ogm.exception.ServiceNotFoundException: Could not load driver: null.

    Any Help Will Be Good.


    You didn't say which version you are using, but if you are using 4.2.1 then the quick start seems to be missing transaction manager definition:

    Add following to your PersistenceContext class:

    @Bean
    public Neo4jTransactionManager transactionManager() {
        return new Neo4jTransactionManager(sessionFactory());
    }
    

    Please refer to spring-data-neo4j documentation for further details.

    http://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#reference.getting_started.spring-configuration

    链接地址: http://www.djcxy.com/p/68776.html

    上一篇: mysql的utf8字符问题

    下一篇: Spring Data Neo4J数据库连接