05-java向Neo4j添加节点及其关系

目标

主要是:(梁川川 男 25 )》 喜欢》 (谢静静 女 25) =》 谢静静拥有》一部魅族手机

05-java向Neo4j添加节点及其关系_第1张图片

maven项目

        
            org.neo4j
            neo4j
            3.2.3
        

项目

package com.lcc.xjj;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;

import java.io.File;

public class Neo4jJavaAPIDBOperation {
    public static void main(String[] args) {
        GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
       // GraphDatabaseService db = dbFactory.newEmbeddedDatabaseBuilder(new File("D:\\neo4j\\install\\data8"))
        //        .setConfig( GraphDatabaseSettings.allow_store_upgrade,"true").newGraphDatabase();

        GraphDatabaseService db= dbFactory.newEmbeddedDatabase(new File("D:\\neo4j\\install\\data8") );
        try (Transaction tx = db.beginTx()) {

            // 创建节点
            Node User1 = db.createNode(Tutorials.User);
            User1.setProperty("name", "梁川川1");
            User1.setProperty("sex", "男");
            User1.setProperty("age", "25");

            // 创建节点
            Node User2 = db.createNode(Tutorials.User);
            User2.setProperty("name", "谢静静1");
            User2.setProperty("sex", "女");
            User2.setProperty("age", "25");

            // 梁川川 喜欢 谢静静  的关系
            Relationship relationship = User1.createRelationshipTo(User2,TutorialRelationships.LOVE);
            // 设置关系属性  梁川川 喜欢谢静静 多久了   开始时间
            relationship.setProperty("time","1年");
            relationship.setProperty("startTime","2017-06-01");


            // 创建节点
            Node Phone = db.createNode(Tutorials.Phone);
            User1.setProperty("brand", "魅族");
            User1.setProperty("money", "1000");
            User1.setProperty("time", "2017-06-01");



            // 谢静静 有一部魅族手机  的关系
            Relationship phoneRelationship = User2.createRelationshipTo(Phone,TutorialRelationships.HIVE);
            // 设置关系属性
            phoneRelationship.setProperty("time","1年");
            phoneRelationship.setProperty("startTime","2017-06-01");


            tx.success();
        }
        // 关闭非常重要
        db.shutdown();
        System.out.println("Done successfully");
    }
}
package com.lcc.xjj;

import org.neo4j.graphdb.RelationshipType;
public enum TutorialRelationships implements RelationshipType{
    LOVE,HIVE;
}
package com.lcc.xjj;

import org.neo4j.graphdb.Label;
public enum Tutorials implements Label {
    User,Phone;
}

可能会报错如下

org.neo4j.kernel.StoreLockException: Store and its lock file has been locked by another process
解决:
http://blog.csdn.net/qq_21383435/article/details/79567344

UpgradeNotAllowedByConfigurationException: Failed to start Neo4j with an older data store version.
解决:
http://blog.csdn.net/qq_21383435/article/details/79567395

StoreUpgrader$UnexpectedUpgradingStoreVersionException: Not possible to upgrade a store with version
解决:
http://blog.csdn.net/qq_21383435/article/details/79567489

你可能感兴趣的:(数据库-neo4j,java,neo4j,写入)