首页 > 解决方案 > 无法使用存储库中添加的功能

问题描述

我正在为我的项目使用 Spring Data Neo4j 2.4.4。这是项目中的一些:

用户

在此处输入图像描述

用户存储库

在此处输入图像描述

我仍然可以使用 Repository 中的内置函数,例如 save()、findAll(),...但是当我添加和使用某些函数时,例如“existsByUsername”,它会出现错误:

11:53:26.562 [http-nio-8081-exec-1] WARN  o.s.d.n.c.Neo4jPersistenceExceptionTranslator - Don't know how to translate exception of type class org.neo4j.driver.exceptions.NoSuchRecordException

然后,我尝试为函数添加查询,它仍然是 在此处输入图像描述

你能帮我确定这个错误并给我一个解决方案吗?谢谢!\

更新: 我在 Postman 中调用 API,我收到了这个结果,而我的数据库只有 1 个用户:

{
    "error": "Records with more than one value cannot be converted without a mapper.; nested exception is java.lang.IllegalArgumentException: Records with more than one value cannot be converted without a mapper."
}

标签: springspring-bootneo4jspring-data-neo4j

解决方案


正如您的例外所述,Neo4j 没有返回任何记录,因此它不能映射到布尔值。

最好是使用Optional<User>并检查isPresent()

@Query("MATCH (n:User {username: $username}) RETURN n")
Optional<User> existsForUsername(String username);

也就是说,它已经由 Spring Data 处理,而不使用自定义查询:

boolean existsByUsername(String username);

参考:https ://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#appendix.query.method.subject


推荐阅读