spring - 无法使用存储库中添加的功能
问题描述
我正在为我的项目使用 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."
}
解决方案
正如您的例外所述,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
推荐阅读
- scala - Scala函数来计算特定的出现次数
- python - 从另一个字典创建自定义字典
- html - CSS网格自动定位
- python - python - 如何使用python绘制相互相邻的图表?
- c# - 使用 Unity 的视频游戏中的多个角色
- kotlin - 尝试在 Kotlin 1.4.10 中找到 KotlinToJVMBytecodeCompiler.compileScript 方法
- r - 如何获得作为参数工作的权力作为函数工作
- javascript - 如何在javascript lwc中停止新日期对象的时区转换?
- javascript - 按对象中的值搜索
- amazon-web-services - AWS IOT 分区设计