简体   繁体   English

Spring Data Neo4j节点NotfoundException

[英]Spring Data Neo4j Node NotfoundException

I am using the newest Spring Data for Neo4j. 我正在为Neo4j使用最新的Spring数据。 In this projects i have different groups which are reachable over the url /group/{id}/project which should return a list of all projects the user has access to. 在这个项目中,我有不同的组,可以通过url / group / {id} / project访问这些组,这些组应该返回用户有权访问的所有项目的列表。 This stuff works fine, but if the user enters a real big number as groupId which does not exist in the database I got a 这个东西工作正常,但是如果用户输入一个真正的大数字作为groupId,而这个数字在数据库中不存在,我会得到一个

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

My query looks like 我的查询看起来像

@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);

Even if I use the method findOne() from the GraphRepository interface I got this exception. 即使我从GraphRepository接口使用findOne()方法,也遇到了此异常。

So is it possible to tell SDN instead of throwing this exception returning null? 那么有可能告诉SDN而不是抛出此返回null的异常吗? Or does i have to catch every possible runtime exception? 还是我必须捕获所有可能的运行时异常?

I want to throw exceptions by my own ie NoSuchGroup, NoSuchUser.. 我想自己抛出异常,即NoSuchGroup,NoSuchUser。

I am using SDN 3.3.0.Release. 我正在使用SDN 3.3.0.Release。

Thank you 谢谢

Which is to be expected, if the node is not found. 如果找不到该节点,那是可以预期的。

You should not use the Neo4j node-id for this but a custom id that you create an manage when you create the Group. 您不应为此使用Neo4j节点ID,而应使用在创建组时创建的自定义ID。

eg 例如

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM