Neo4j with Spring - 最佳存储库设计

Neo4j with Spring - Best Repository Design

目前我正在研究基于 Spring 数据 Neo4j 的项目。在大多数节点中,可以有多种类型的不同关系,如下例所示。

节点定义

    @NodeEntity(label = CollectionNames.User)
    public class User{

        @GraphId
        private Long id;

        //Different Parameters, removed because these are irrelevant
        @Relationship(type = RelationshipNames.HAS_CONTACT, direction = Relationship.OUTGOING)
        private Set<HasContact> contactList;

        @Relationship(type = RelationshipNames.HAS_INVITED, direction = Relationship.OUTGOING)
        private Set<HasInvited> invitedContacts;

        @Relationship(type = RelationshipNames.HAS_FAVORITE, direction = Relationship.OUTGOING)
        private Set<HasFavorite> favoriteMerchants;

    //And so many others such relationships

    //Constructor and Getters() & Setters()
}

存储库定义

@Repository
public interface UserRepository extends GraphRepository<User>{

    List<User> findByUsernameIn(Set<String> username, @Depth int depth);//Here for exp, I just want to load user with his/her 'contactList' entries only
    User findByUsername(String username, @Depth int depth);
}

虽然此存储库在加载具有给定深度的给定用户时工作正常,但主要问题是此查询将加载所有现有关系直至达到给定深度。在这里我只对某些/或一种特定类型的关系感兴趣,那么如何使用 Spring 命名查询方法实现呢?我可以在使用命名查询方法加载时为每个关系指定深度吗?或者我必须使用 @Query 注释为每个这样的关系编写自定义查询?我们希望尽量减少自定义查询的使用!

那么,对于这种情况,Spring Data Neo4j 中最好的存储库设计是什么? 非常感谢您的建议!

从 SDN 版本 4 开始。2.x 如果您想避免加载所有相关实体,在 @Query 中或使用 session.query 自定义密码查询是您的最佳选择。

在即将发布的版本中正在进行更细粒度的加载工作。看到这个 github issue.