neo4j java cypher参数无法正常工作

时间:2018-03-26 08:20:17

标签: neo4j cypher

我正在尝试在图中创建一些虚拟节点:

    private final static Driver driver = GraphDatabase.driver("bolt://localhost:7687",
        AuthTokens.basic("neo4j", "password"));
    static Session session = driver.session();

    String cypher = "CREATE "
            + "(:GPPocEntity {id:'{gppeid}',gppe_out_prop_1:'{gppe_out_prop_1_val_id}',"
            +                           "gppe_out_prop_2:'{gppe_out_prop_2_val_id}',"
            +                           "gppe_out_prop_X:'{gppe_out_prop_X_val_id}'})"
            + "-[:has]->"
            + "(:PPocEntity {id:'{ppeid}',ppe_out_prop_1:'{ppe_out_prop_1_val_id}',"
            +                           "ppe_out_prop_2:'{ppe_out_prop_2_val_id}',"
            +                           "ppe_out_prop_X:'{ppe_out_prop_X_val_id}'})"
            + "-[:contains]->"
            + "(:PocEntity {id:'{peid}',pe_out_prop_1:'{pe_out_prop_1_val_id}',"
            +                           "pe_out_prop_2:'{pe_out_prop_2_val_id}',"
            +                           "pe_out_prop_X:'{pe_out_prop_X_val_id}'})";

    Map<String, Object> params = new HashMap<String, Object>();
    int id = 1111;
    params.put("gppeid","gppe"+id);
    params.put("ppeid","ppe"+id);
    params.put("peid","pe"+id);
    params.put("gppe_out_prop_1_val_id","gppe_out_prop_1_val_"+id);
    params.put("gppe_out_prop_2_val_id","gppe_out_prop_2_val_"+id);
    params.put("gppe_out_prop_X_val_id","gppe_out_prop_X_val_"+id);
    params.put("ppe_out_prop_1_val_id","ppe_out_prop_1_val_"+id);
    params.put("ppe_out_prop_2_val_id","ppe_out_prop_2_val_"+id);
    params.put("ppe_out_prop_X_val_id","ppe_out_prop_X_val_"+id);
    params.put("pe_out_prop_1_val_id","pe_out_prop_1_val_"+id);
    params.put("pe_out_prop_2_val_id","pe_out_prop_2_val_"+id);
    params.put("pe_out_prop_X_val_id","pe_out_prop_X_val_"+id);

    session.run(cypher, params);

但是这并没有在密码中设置这些参数。为什么会这样?

2 个答案:

答案 0 :(得分:1)

问题是您使用单引号将cypher-query中的参数包装起来,因此不会解释它们。尝试通过删除单引号来更正查询:

String cypher = "CREATE "
        + "(:GPPocEntity {id:{gppeid}, gppe_out_prop_1: {gppe_out_prop_1_val_id},  "
        + "                            gppe_out_prop_2: {gppe_out_prop_2_val_id},  "
        + "                            gppe_out_prop_X: {gppe_out_prop_X_val_id}}) "
        + "-[:has]->"
        ...

答案 1 :(得分:0)

cypher参数为$ +名称,而不是{ +名称+ }。 因此,对于名为gppe_out_prop_1_val_id的参数,您应该将$gppe_out_prop_1_val_id放入查询中。

而且你不需要输入引号,键入参数,所以Neo4j会为你做。