使用带有 Spring Data Neo4j 的远程 Neo4j 服务器时不保存数据

Data is not saved when using remotely Neo4j Server with Spring Data Neo4j

我使用 spring-data-neo4j 创建了一个 Maven 项目。我还安装了独立的 Neo4j Server Community Edition 2.3.3。我试图将一些 Vertex 对象保存到数据库中,然后简单地检索它们以检查一切是否正常。然后,我希望能够在独立服务器中打开创建的数据库以获得更好的可视化效果。 我正在使用作为依赖项:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency> 

配置class为:

@Configuration
@ComponentScan("neo4j.example")
@EnableAutoConfiguration
@EnableNeo4jRepositories("neo4j.example.repository")

public class App extends Neo4jConfiguration {

    public App() {
        System.setProperty("username", "neo4j");
        System.setProperty("password", "root");
    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("neo4j.example.model");
    }

    @Override
    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer("http://localhost:7474");
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }


    }

我的 NodeEntity 看起来像:

@NodeEntity
public class Vertex  {

    private String name;
    @GraphId
    private Long id;

    @Relationship(type = "PAIRS_WITH", direction = "UNDIRECTED")
    public  Set<Vertex> teammates;

    public Vertex() {   }

// getters, setters, equals, toString
    }

存储库:

@Repository
public interface VertexRepository extends GraphRepository<Vertex> {

    Vertex findByName(String name);

    List<Vertex> findByTeammatesName(String name);
}

服务:

@Service
public class VertexServiceImpl implements VertexService {

    @Autowired
    private VertexRepository vertexRepository;

    @Override
    @Transactional
    public Vertex create(Vertex vertex) {
        return vertexRepository.save(vertex);
    }

    @Override
    @Transactional
    public Iterable<Vertex> findAll() {
        return vertexRepository.findAll();
    }
//....
}

然后我有一个控制器,有两个简单的方法来保存顶点然后查询数据库。

@RestController
@RequestMapping("/api/")
public class GraphController {

    @Autowired
    VertexService vertexService;

    @RequestMapping(value = "addvertex", method = RequestMethod.GET)
    public void add() {
        Vertex v = new Vertex();
        v.setId(1l);
        v.setName("name");
        Vertex v2 = new Vertex();
        v2.setId(2l);

        v.worksWith(v2);
        vertexService.create(v);
    }

    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Iterable<Vertex> getAll() {
        return vertexService.findAll();
    }

}

当我将顶点保存到数据库时没有错误。当我调用 /all 时,数据库是空的。我检查了 messages.log 并且没有例外......最后一行是:

2016-03-26 14:25:15.716+0000 INFO  [o.n.k.i.DiagnosticsManager] Interface Microsoft Wi-Fi Direct Virtual Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000:
2016-03-26 14:25:15.716+0000 INFO  [o.n.k.i.DiagnosticsManager] --- INITIALIZED diagnostics END ---
2016-03-26 14:25:15.747+0000 INFO  [o.n.k.i.DiagnosticsManager] --- STOPPING diagnostics START ---
2016-03-26 14:25:15.747+0000 INFO  [o.n.k.i.DiagnosticsManager] --- STOPPING diagnostics END ---
2016-03-26 14:25:15.747+0000 INFO  [o.n.k.i.f.GraphDatabaseFacade] Shutdown started

非常感谢任何帮助!

您要混合使用嵌入式服务器和远程服务器吗?

您应该在远程服务器中查找您的数据。

此外,您必须禁用身份验证才能在服务器中运行,或者您必须为您的配置提供用户名 (neo4j) 和密码。

不要在服务器使用的同一目录中启动嵌入式数据库

我设法解决了我的问题。配置很好,问题是我试图设置 @NodeEntity 对象的 id 属性。即使我删除了 @GraphId 属性 顶点也不会被保存。 This post 解决了同样的问题。
In Good Relations:The Spring Data Neo4j Guide Book 提到:"If the field is simply named 'id' then it is not necessary to annotate it with @GraphId as the OGM will use it automatically."

如果有某种警告/错误消息或在文档中更明确地提到您不能 setId() 并且如果您这样做节点将不会保存在数据库中,那就太好了。希望这会节省一些时间!