Neo4J – Simple cypher query
假设有两个节点实体:
1
2 3 4 5 6 7 8 9 10 11 12 13 |
public class Account extends BaseEntity
{ … @Fetch @RelatedTo(type =”HAS_ROLE”) private Set<Role> roles = Sets.newHashSet(); … } public class Role extends BaseEntity |
在我的存储库中,我有一个查询应该按给定角色获取所有帐户:
1
2 3 4 5 |
public interface AccountRepository extends GraphRepository<Account>
{ @Query(“START account=node:Account(0) MATCH account-[:HAS_ROLE]->({0}) return account”) Iterable<Account> findByRole(Role role); } |
但是这个查询不起作用,当我在我的测试用例中使用这个方法时,我得到以下错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement START account=node:Account(0) MATCH account-[:HAS_ROLE]->({0}) return account; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement START account=node:Account(0) MATCH account-[:HAS_ROLE]->({0}) return account; nested exception is expected string
看来,我的查询有问题,但我不知道是什么,也无法弄清楚…
谁能提供一些帮助?
- 请指定使用的 Neo4j 版本,这很重要。
- 我正在使用 neo4j 1.8.M07、spring-data-neo4j 2.1.0.BUILD-SNAPSHOT
- 请接受您自己的答案或答案(并接受)。看起来你有一个解决方案(“我把它改成……一切都很好”),最好能解决这个问题!
像这样重写您的查询。您已经知道该角色,因此请将其用作起点。
1
2 |
@Query(“START role=node({0}) MATCH account-[:HAS_ROLE]->role return account”)
Iterable<Account> findByRole(Role role); |
使用这个查询怎么样?
1
|
START account=node(*) MATCH (account)-[r:HAS_ROLE]->() return account
|
将返回所有具有”HAS_ROLE”的帐户
马库斯,
您应该迁移到 Neo4j 1.8 GA 和 SDN 2.1.0 RELEASE。
另外,您的 BaseEntity 和 Role 类是什么样的?
问候,
拉塞
- 嘿 Lasse,感谢您的帮助..我切换到当前版本,但我的查询似乎是错误的。我把它改成了”START role=node({0}) MATCH account-[:HAS_ROLE]->role return account”(感谢Michael Hunger),一切都很好……真的需要做一些Neo4j的功课。 ..
来源:https://www.codenong.com/12920258/