2013-05-21 53 views
0

我有一个简单的关系测试,我试图运行使用Rest API创建一个独特的节点(java-rest-binding)https://github.com/neo4j/java-rest-binding但不幸的是我被困在某些东西上,这里是细节:(非唯一的节点和关系工作完全正常,与这一点,它没有,很有可能我做的东西幼稚(原谅我缺乏的Neo4j的知识)getOrCreate for java-rest-api neo4j失败

final UserModel userModel = new UserModel(); 
     final HashMap<String, Object> uModelAttributes = new HashMap<String, Object>(0); 
     uModelAttributes.put("name", "AnirudhVyas"); 
     userModel.setAttributes(uModelAttributes); 
     final HashSet<Action> buyHistory = new HashSet<Action>(); 
     final Action buyAction = new Action(); 
     final ProductModel productModel = new ProductModel(); 
     final HashMap<String, Object> attributes = new HashMap<String, Object>(0); 
     attributes.put("name", "mercedes benz "); 
     attributes.put("make", "mercedes benz"); 
     attributes.put("model", "sls 550"); 
     attributes.put("year", "2014"); 
     productModel.setAttributes(attributes); 
     buyAction.setProduct(productModel); 
     buyHistory.add(buyAction); 
     userModel.setBuyHistory(buyHistory); 
     System.out.println("Before"); 
     new UserModelDAO().createCompleteTree(userModel); 
     System.out.println("Completed >>> 

,如果我。使用此道:

final RestNode root = api.getOrCreateNode(api.index().forNodes("users", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext")), "name", m 
        .getAttributes().get("name"), m.getAttributes()); 

api.getOrCreateNode(api.index().forNodes("products", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext")), "name", buyAction.getProduct().getAttributes().get("name"), buyAction.getProduct().getAttributes()), RelationshipTypes.BOUGHT); 

这基本上失败:

java.lang.RuntimeException: Error retrieving or creating node for key name and value AnirudhVyas with index users 
     at org.neo4j.rest.graphdb.ExecutingRestAPI.getOrCreateNode(ExecutingRestAPI.java:448) 
     at org.neo4j.rest.graphdb.RestAPIFacade.getOrCreateNode(RestAPIFacade.java:223) 
     at xxxx.xxxx.xxxx.graph.UserModelCreateTasteKeyNeo4JBatchCallback.recordBatch(UserModelCreateTasteKeyNeo4JBatchCallback.java:61) 

回答

0

有几种方法可以做到这一点,其中之一是使用暗号:

MATCH a 
WHERE a.name! = 'nameTofound' 
CREATE UNIQUE a-[:Relationship]-c:LABEL 
RETURN c 

使用它queryEngine内。 它的工作原理就像一个魅力,请看看下面的链接了解详情: http://docs.neo4j.org/chunked/milestone/query-create-unique.html

Java代码是在这里:

protected static RestNode createOrGetExistingNode(String key, String valueFor, String Rel, String label){ 
    RestNode node = null; 
    final QueryResult<Map<String, Object>> result = GraphRestService.queryEngine.query(String.format("MATCH node WHERE node.%s! ='%s' " + 
      "CREATE UNIQUE node-[:%s]-c:%s RETURN c" , key, valueFor, Rel, label), MapUtil.map("reference", 0)); 
    for (Map<String, Object> column : result) { 
     node = (RestNode) column.get("c"); 
    } 
    return node; 
} 
0

我不使用批处理休息回调解决了这个问题 - 当我简单地使用 - getOrCreateXXX()用于RESTAPI它就像一个魅力 - 需要进一步调查,为什么getOrCreate上BatchCallback#recordBatch()的行为不同。

+0

的批处理操作只是记录你的电话,然后重播他们的执行你的块,所以任何类型的读数只有延迟到块被实际执行。 –

+0

关注更多细节? –