【问题标题】:Spring Data Neo4j Node NotfoundExceptionSpring Data Neo4j 节点 NotfoundException
【发布时间】:2015-06-23 19:02:54
【问题描述】:

我正在为 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 接口中的 findOne() 方法,我也会遇到此异常。

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

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

我正在使用 SDN 3.3.0.Release。

谢谢

【问题讨论】:

    标签: java spring neo4j


    【解决方案1】:

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

    您不应为此使用 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      相关资源
      最近更新 更多