在同一测试中创建时,使用graphDatabaseService找不到节点

在同一测试中创建时,使用graphDatabaseService找不到节点

问题描述:

继续尝试neo4j grails插件,但它没有文件

我现在有一个测试看起来像这样:

I have a test now looks like this:

package com.iibs.graph

import groovy.util.GroovyTestCase
import com.iibs.graph.Node

public class NodeTests extends GroovyTestCase {

    def graphDatabaseService

    void testCRUD() {
        Node.deleteAll(Node.list())

        Node node = new Node(name: "Name")

        node.save(flush: true, failOnError: true)

        Node found = Node.findByName("Name")

        assert found instanceof Node
        assert found.getName() == "Name"

        org.neo4j.graphdb.Node graphNode = graphDatabaseService.getNodeById(node.getId())

        assert graphNode.instanceOf(org.neo4j.graphdb.Node) == true
    }
}

我收到如下错误:

| Running 1 integration test... 1 of 1
| Failure:  testCRUD(com.iibs.graph.NodeTests)
|  org.neo4j.graphdb.NotFoundException: Node 905482810884096 not found
    at org.neo4j.kernel.InternalAbstractGraphDatabase.getNodeById(InternalAbstractGraphDatabase.java:1088)
    at com.iibs.graph.NodeTests.testCRUD(NodeTests.groovy:22)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
| Completed 1 integration test, 1 failed in 0m 2s

据我所知,应该是获得节点的可能方式之一。或者我错了?

As far as I understand from the mentioned thread, this should be one of the possible ways to get the node. Or am I wrong?

尝试下面的代码片段(没有测试自己,只是一个braindump):

Try the following snippet (did not test myself, just a braindump):

def nodeInstance = ....
nodeInstance.save(flush:true)

String label = nodeInstance.getClass().simpleName
def neoNode = graphDatabaseService.findNodesByLabelAndProperty(DynamicLabel.label(label), "__id__", nodeInstance.id)