java - Spring Data Neo4j 节点 NotfoundException

标签 java spring neo4j

我正在使用 Neo4j 的最新 Spring Data。 在这个项目中,我有不同的组,可以通过 url/group/{id}/project 访问,该组应该返回用户有权访问的所有项目的列表。这个东西工作正常,但是如果用户输入一个真正的大数字作为 groupId,而数据库中不存在,我会得到一个

org.neo4j.graphdb.NotFoundException:找不到节点 400

我的查询看起来像

@Query("START a=node({userId}), b=node({groupId}) match a-[:user_belongs_to]-b return b")
GroupEntity getGroup(@Param("userId") Long userId, @Param("groupId") Long groupId);

即使我使用 GraphRepository 接口(interface)中的 findOne() 方法,我也会遇到此异常。

那么是否可以告诉SDN而不是抛出这个返回null的异常?或者我是否必须捕获所有可能的运行时异常?

我想自己抛出异常,即NoSuchGroup、NoSuchUser..

我正在使用 SDN 3.3.0.Release。

谢谢

最佳答案

如果未找到节点,这是可以预料的。

您不应为此使用 Neo4j 节点 ID,而应使用在创建组时创建的自定义 ID。

例如

@NodeEntity
class Group {

   @GraphId Long id;
   @Indexed int groupId;

   @RelatedTo(type="user_belongs_to",direction=INCOMING)
   Set<User> users;

}

interface GroupRepository extends GraphRepository<Group> {
   @Query("match (a:User)-[:user_belongs_to]-(b:Group) where a.userId = {userId} and b.groupId={groupId} return b")
   GroupEntity getGroup(@Param("userId") Long userId, @Param("groupId") Long groupId);

   // generated finder method
   GroupEntity findByGroupIdAndUsersUserId(@Param("groupId") Long groupId, @Param("userId") Long userId);
}

关于java - Spring Data Neo4j 节点 NotfoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011295/

相关文章:

java - 条件注入(inject)bean

indexing - node_auto_index 不存在

java - 如何在java(Excel)中写入特定列并读取特定列?

java - 如何在 java 中使用扫描器类捕获空白输入

java - 使用 Spring 和 Jdbc 访问数据库

java - Java中获取当前Play session()

java - @事务: Not committing at end of transaction; using Spring Boot and neo4j

neo4j - 如何从 Neo4J 中的子字符串创建关系

java - 如何使用ini4j编写ini文件

java - 应用程序中有两种不同的架构