Neo4j 使用Spring数据更新节点对象
我想更新数据库中现有的节点。 我可以正确创建节点,但无法更新现有节点Neo4j 使用Spring数据更新节点对象,neo4j,spring-data-neo4j,Neo4j,Spring Data Neo4j,我想更新数据库中现有的节点。 我可以正确创建节点,但无法更新现有节点 try (Transaction tx = template.getGraphDatabaseService().beginTx()) { Node node = repository.findNodeUsingId("n1"); if(node != null){ //Modify some properties using setProperty node.setP
try (Transaction tx = template.getGraphDatabaseService().beginTx()) {
Node node = repository.findNodeUsingId("n1");
if(node != null){
//Modify some properties using setProperty
node.setProperty("name","P");
//How should I do to save the modified node object?
}else{
//Create the node
//This part works fine
node = template.createNode();
node.setProperty("name", "T");
}
tx.success();
}
您不必保存修改过的对象 调用setProperty后,您的节点属性已在当前事务中设置 此处缺少的唯一一件事是关闭事务,请检查(来自)关于
事务的内容。关闭()
:
提交或标记此事务以进行回滚,具体取决于
先前已调用success()或failure()。全部的
从内部执行的操作返回的ResourceRables
此交易记录将通过此方法自动关闭。这
方法来自AutoCloseable,以便事务可以参与
在try with resource语句中。它不会抛出任何声明
例外。调用此方法(在
try with resource语句)或finish()具有完全相同的效果
我的错。在我的代码中有一个示例中,我错过了tx.success(),因此您提出的解决方案对我不起作用。你的解决方案是正确的