[英]Spring Data Neo4j Node NotfoundException
我正在為Neo4j使用最新的Spring數據。 在這個項目中,我有不同的組,可以通過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。
謝謝
如果找不到該節點,那是可以預期的。
您不應為此使用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.