0

我正在尝试在我的 Neo4j 数据库中创建节点和关系。创建了节点,因为我可以打印出节点 ID,但它们不会持续存在。我的程序的下一个阶段是计算索引中的节点数,这会产生 NullPointerExpection 错误。我正在使用 Gephi 来可视化图形,它既不显示节点也不显示关系。如何让节点持久存在?这可能是因为我有另一个方法(由于长度未显示)包装在事务中,它调用这些节点创建方法?谢谢

以下是部分代码:

public class Neo4JGraphDbClass {

private String graphLocation = "D:\\testdb"  ;

private GraphDatabaseService graphDb ;
private IndexManager graphIndex ;

private Index<Node> property1;
private Index<Node> special_nodes ;

public Neo4JGraphDbClass() {}
public void createGraphDb() {
    graphDb = new EmbeddedGraphDatabase(graphLocation) ;
    graphIndex  =  graphDb.index();
    property1  = graphIndex.forNodes("PROPERTY1");
    special_nodes = graphIndex.forNodes("SPECIAL_NODES");
}

public Node createAndIndex(Integer[] property1) {
    Transaction transaction0 = graphDb.beginTx();
    try {
        Node node =    graphDb.createNode();
        node.setProperty("PROPERTY1",property1) ;
        property1.putIfAbsent(node, "PROPERTY1", property1);
        System.out.println(node.getId());
        transaction0.success();
        return node;
    } finally {
        transaction0.finish();
    }
}

public Node createSpecialNodes(Integer name) {
    Transaction transaction1 = graphDb.beginTx();
    try {
        Node node = graphDb.createNode();
        node.setProperty("SPECIAL_NODE", name);
        special_nodes.add(node, "SPECIAL_NODE", name);
        transaction1.success();
        return node;
    }
    finally {
        transaction1.finish();
    }
}
public void addNodesToGraph(Integer preName, ArrayList<Integer[]> preProperty1) {
    //stuff to change preName into name and preProperty into property
Transaction transaction = graphDb.beginTx();
    try {
        Node specialNode = createSpecialNodes(name) ;
        Node previousNode = graphDb.getReferenceNode();
        for (int i = 0; i < property1.size(); i++) {
            Node currentNode =  createAndIndex(property1.get(i));
            previousNode.createRelationshipTo(currentNode, RelTypes.NEXT_MEASURE);
            currentNode.createRelationshipTo(specialNode,RelTypes.SAMPLE);
            previousNode = currentNode; }
        transaction.success();
    }
    finally {
        transaction.finish();
    }
}

public Integer countIndex() {
    Integer hitSize;
    Transaction tx = graphDb.beginTx();
    try {
        IndexHits<Node> hits = graphIndex.forNodes("PROPERTY1").get("PROPERTY1", "*");
        hitSize = hits.size();
        tx.success();
    }
    finally {
        tx.finish();
    }
    return hitSize;
}

public Integer indexSize(Integer lookup) throws NullPointerException{
    Integer hitSize;

    Transaction tx2 = graphDb.beginTx();
    try {

        IndexHits<Node> hits =  graphIndex.forNodes("PROPERTY1").query(lookup);
        hitSize = hits.size();
        tx2.success();
    }
    finally {
        tx2.finish();
    }
    return hitSize;
}
4

1 回答 1

0

代码看起来没问题。您的查询是什么样的,即您如何得出数据未持久化的结论?

于 2012-05-18T10:29:28.400 回答