neo4j创建图数据库

时间:2012-07-08 16:14:12

标签: neo4j

package com;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.graphdb.Transaction;


public class hotspots {
public static enum RelTypes implements RelationshipType
{
    PERSON
}
public static void main(String[] args) {
    GraphDatabaseService graphdb = new EmbeddedGraphDatabase("target/dbnew");
    Transaction tx = graphdb.beginTx();

    try{
        Node n1 = graphdb.createNode();
        Node n2 = graphdb.createNode();

        n1.setProperty("name","Melwin");
        n2.setProperty("name","Louis");

Relationship rel1 =      graphdb.getReferenceNode().createRelationshipTo( n1, RelTypes.PERSON );
Relationship rel2 = graphdb.getReferenceNode().createRelationshipTo( n2,  RelTypes.PERSON );

        tx.success();
    }
    catch (Exception e) {
        tx.failure();
    }
    finally{
        tx.finish();
    }

    graphdb.shutdown();
    System.out.println("Success");
}

}

这是一个我创建的小型数据库......我在neoclipse中查看...每次运行此代码并在neoclipse中查看它...我得到节点n关系的两倍...即。我得到另外两个名字相同的节点&关系..... 我正在做一个重要的项目,需要帮助尽快... 所以伙计们帮助我!!!

1 个答案:

答案 0 :(得分:2)

只创建具有与数据库中已存在的其他节点相同属性的新节点将创建重复项。关系也是如此。如果您正在导入数据,那么只执行一次,或者您的导入代码必须是create-if-not-already-exists“aware”,在检查节点是否存在之前检查索引并在创建之前关系检查是否已存在,通过为该节点迭代它们。