无法在查询中传递第二个参数

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

Unable to pass a second param in query

问题

以下是翻译好的部分:

以下查询可以正常工作。

interface MyRepository : Neo4jRepository<Character?, Long?> {
    @Query("MATCH (c1:Character)-[:positive]->(c2:Character) WHERE c1.id = $id RETURN c2")
    fun findRelations(relation: String, id: Long): List<Character>
}
但是我也想将关系作为参数传递。但是当我执行这个操作时,出现了错误。

interface MyRepository : Neo4jRepository<Character?, Long?> {
    @Query("MATCH (c1:Character)-[:$relation]->(c2:Character) WHERE c1.id = $id RETURN c2")
    fun findRelations(relation: String, id: Long): List<Character>
}
错误。

> org.springframework.dao.InvalidDataAccessResourceUsageException:
> 无效输入 '$': 预期 "%", "(", 或标识符 (第1行,第24列 (偏移量: 23))\n"MATCH
> (c1:Character)-[:$relation]->(c2:Character) WHERE c1.id = $id RETURN
> c2"\n                        ^; 错误代码
> 'Neo.ClientError.Statement.SyntaxError
所以我尝试使用建议的 %。但那也引发了错误。`\%` 引发编译错误,因此使用双 `\\`。

interface MyRepository : Neo4jRepository<Character?, Long?> {
    @Query("MATCH (c1:Character)-[:\\%relation]->(c2:Character) WHERE c1.id = $id RETURN c2")
    fun findRelations(relation: String, id: Long): List<Character>
}
错误。

> org.springframework.dao.InvalidDataAccessResourceUsageException:
> 无效输入 '\\': 预期 "%", "(", 或标识符 (第1行,第25列 (偏移量: 24))\n"MATCH
> (c1:Character)-[:\\%relation]->(c2:Character) WHERE c1.id = $id RETURN
> c2"\n                         ^; 错误代码
> 'Neo.ClientError.Statement.SyntaxError

我漏掉了什么,哪里出现了语法错误?

英文:

The following query works fine.

interface MyRepository : Neo4jRepository<Character?, Long?> {
    @Query("MATCH (c1:Character)-[:positive]->(c2:Character) WHERE c1.id = $id RETURN c2")
    fun findRelations(relation: String, id: Long): List<Character>
}

But I want to pass that relation as a param too.

But when I perform this I end up with errors.

interface MyRepository : Neo4jRepository<Character?, Long?> {
    @Query("MATCH (c1:Character)-[:$relation]->(c2:Character) WHERE c1.id = $id RETURN c2")
    fun findRelations(relation: String, id: Long): List<Character>
}

The error.

> org.springframework.dao.InvalidDataAccessResourceUsageException:
> Invalid input '$': expected "%", "(" or an identifier (line 1,
> column 24 (offset: 23))\n"MATCH
> (c1:Character)-[:$relation]->(c2:Character) WHERE c1.id = $id RETURN
> c2"\n ^; Error code
> 'Neo.ClientError.Statement.SyntaxError

So I tried with % as suggested. That throws error too. \% throws compilation error thus using double \\.

interface MyRepository : Neo4jRepository<Character?, Long?> {
    @Query("MATCH (c1:Character)-[:\\%relation]->(c2:Character) WHERE c1.id = $id RETURN c2")
    fun findRelations(relation: String, id: Long): List<Character>
}

The error.

> org.springframework.dao.InvalidDataAccessResourceUsageException:
> Invalid input '\': expected "%", "(" or an identifier (line 1,
> column 25 (offset: 24))\n"MATCH
> (c1:Character)-[:\%relation]->(c2:Character) WHERE c1.id = $id RETURN
> c2"\n ^; Error code
> 'Neo.ClientError.Statement.SyntaxError

What am I missing, what is the syntax error?

答案1

得分: 1

Cypher不支持路径模式中的动态关系类型。尽管如此,您仍然可以在Cypher查询中使用动态关系类型(尽管效率可能不高)。

这应该适用于您:

@Query("MATCH (c1:Character)-[r]->(c2:Character) WHERE TYPE(r) = $relation AND c1.id = $id RETURN c2")
英文:

Cypher does not support dynamic relationship types in a path pattern. Nevertheless, you still can use dynamic relationship types in a Cypher query (although it will not be efficient).

This should work work you:

@Query("MATCH (c1:Character)-[r]->(c2:Character) WHERE TYPE(r) = $relation AND c1.id = $id RETURN c2")

huangapple
  • 本文由 发表于 2023年3月12日 07:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710280.html
匿名

发表评论

匿名网友

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

确定