我们如何在Spring Data Neo4j中使用手动查询创建一个节点?

huangapple go评论51阅读模式
英文:

How can we create an node with manual query in spring data neo4j?

问题

以下是要翻译的代码部分:

创建一个节点通过repo接口手动创建但是neo4j驱动程序内部会引发异常

Caused by: org.neo4j.driver.exceptions.ClientException: 无效输入 '{'预期参数 (第1行第22列 (偏移量21))
"CREATE(a:Association {a.name:{0}}) RETURN true"
                       ^

以下是代码片段

@Repository
public interface AssociationRepo extends Neo4jRepository {

 @Query("CREATE(a:Association {a.name:{0}}) RETURN a") -- 这一行有什么问题
 Association addAssociation(String name);

}

调用者代码

@Override
public boolean addAssociationToCountry(String countryCode, Association association) {    

 repo.addAssociation(association.getName());    
 return true;

}
英文:

Create an node manually via repo interface but neo4j driver internally raise an exception as

<pre><code>
Caused by: org.neo4j.driver.exceptions.ClientException: Invalid input '{': expected a parameter (line 1, column 22 (offset: 21))
"CREATE(a:Association {a.name:{0}}) RETURN true"
^
</pre></code>

Below is the code snippet:

<pre><code>
`@Repository
public interface AssociationRepo extends Neo4jRepository {

 @Query(&quot;CREATE(a:Association {a.name:{0}}) RETURN a&quot;) -- what is wrong in this line ?
 Association addAssociation(String name);

}`

</pre></code>

Caller code :

<pre><code>
` @Override
public boolean addAssociationToCountry(String countryCode, Association association) {

 repo.addAssociation(association.getName());    
 return true;

}`
</pre></code>

I checked whole of internet but didn't find any solution.`

答案1

得分: 1

你的语法几乎正确。问题在于花括号内部使用的节点名称。

应该是(a:Association {name:{0}}),而不是(a:Association {a.name:{0}}),因为节点边界已经定义了属性属于这个节点。

英文:

You are very close to the right syntax. The problem is the node name, you are using within the curly brackets.

Instead of (a:Association {a.name:{0}}) it should be (a:Association {name:{0}}) because the node boundaries already define that the properties belong to this very node.

huangapple
  • 本文由 发表于 2023年3月7日 17:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定