Spring数据neo4j

时间:2015-06-30 00:36:13

标签: spring neo4j spring-data-neo4j

我试图使用Neo4j,但我仍有一些问题。当我尝试运行测试来创建节点时,我有一个例外,说无法自动连接存储库接口。

这是我的配置:

build.gradle:

buildscript {
    ext {
        springBootVersion = '1.2.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
        classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot' 
apply plugin: 'io.spring.dependency-management' 

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenCentral()
}


dependencies {
    compile("org.springframework.boot:spring-boot-starter")
    testCompile("org.springframework.boot:spring-boot-starter-test") 
    compile 'org.springframework.data:spring-data-neo4j:3.3.0.RELEASE'
    compile("org.hibernate:hibernate-validator")
    compile 'org.neo4j:neo4j:2.2.3'
    compile 'javax.annotation:javax.annotation-api:1.2'
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-3.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.****.pocneo4j.service.impl" />
    <neo4j:config storeDirectory="/data"
        base-package="com.****.pocneo4j.domain.entity" />
    <neo4j:repositories base-package="com.****.pocneo4j.domain.repository" />
    <tx:annotation-driven />

</beans>

我的实体:

package com.****.pocneo4j.domain.entity;

import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;

@NodeEntity
public class QuestionNode {

    @GraphId
    private Long idQuestionNode;
    private int idQuestionRef;

    public Long getIdQuestionNode() {
        return idQuestionNode;
    }

    public int getIdQuestionRef() {
        return idQuestionRef;
    }

    public void setIdQuestionRef(int idQuestionRef) {
        this.idQuestionRef = idQuestionRef;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((idQuestionNode == null) ? 0 : idQuestionNode.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        QuestionNode other = (QuestionNode) obj;
        if (idQuestionNode == null) {
            if (other.idQuestionNode != null)
                return false;
        } else if (!idQuestionNode.equals(other.idQuestionNode))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "QuestionNode [idQuestionNode=" + idQuestionNode
                + ", idQuestionRef=" + idQuestionRef + "]";
    }

}

存储库

package com.****.pocneo4j.domain.repository;

import org.springframework.data.neo4j.repository.GraphRepository;

import com.****.pocneo4j.domain.entity.QuestionNode;

public interface QuestionNodeRepository extends GraphRepository<QuestionNode>{

}

我的服务:

package com.****.pocneo4j.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.****.pocneo4j.domain.entity.QuestionNode;
import com.****.pocneo4j.domain.repository.QuestionNodeRepository;
import com.****.pocneo4j.service.interf.QuestionNodeIService;

@Service("QuestionNodeManager")
public class QuestionNodeService implements QuestionNodeIService {

    @Autowired
    private QuestionNodeRepository questionNodeRepository;

    @Override
    public QuestionNode create(QuestionNode questionNode){
        return questionNodeRepository.save(questionNode);
    }
}

在这里我的测试

package com.****.pocneo4j.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.****.pocneo4j.domain.entity.QuestionNode;
import com.****.pocneo4j.service.interf.QuestionNodeIService;

public class Test01 {

    @Test
    public void test() {
        @SuppressWarnings("resource")
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        QuestionNodeIService questionNodeManager = (QuestionNodeIService) ctx
                .getBean("QuestionNodeManager");

        QuestionNode testQuestionNode = new QuestionNode();
        testQuestionNode.setIdQuestionRef(90);
        questionNodeManager.create(testQuestionNode);
        System.out.println("NODE ID = " + testQuestionNode.getIdQuestionNode()
                + " || QUESTION REF ID = "
                + testQuestionNode.getIdQuestionRef());

    }

}

0 个答案:

没有答案