【问题标题】:Neo4j with Spring - Best Repository DesignNeo4j with Spring - 最佳存储库设计
【发布时间】:2017-07-04 06:26:51
【问题描述】:

目前我正在研究基于 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 Named Query 方法怎么可能呢?我可以在使用命名查询方法加载时为每个关系指定深度吗?或者我必须使用 @Query 注释为每个这样的关系编写自定义查询?我们希望尽量减少自定义查询的使用!

那么对于这种情况,Spring Data Neo4j 中最好的存储库设计是什么? 建议将不胜感激!

【问题讨论】:

    标签: spring spring-boot neo4j spring-data-neo4j spring-repositories


    【解决方案1】:

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

    在即将发布的版本中,我们正在处理更细粒度的加载。看到这个github issue

    【讨论】:

    • 好的,谢谢回复!但是你能告诉我这个功能将在哪个版本中实现吗?或者需要多长时间?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2012-09-17
    相关资源
    最近更新 更多