3
votes

Currently I am working on project based on Spring data Neo4j. Here in most nodes, there can be multiple type of different relationships like given in below example.

Node definition

    @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 Definition

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

Although this repository is working fine while loading given user with given depth, but main issue is that this query will load all existing relationships with up to given depth. As here I am just interested in some/ or just one particular type of relationship, so how can that be possible using Spring Named Query methods? Can I specify depth for each relationship while loading using Named Query methods? Or I have to write custom query for each such relationship using @Query annotation? We want to minimize usage of Custom queries!

So what is the best Repository Design in Spring Data Neo4j for such cases? Suggestion will be highly appreciated!

1

1 Answers

3
votes

As of SDN version 4.2.x custom cypher query in @Query or by using session.query is you best option if you want to avoid loading all related entities.

Work on more fine grained loading is being done in the upcoming release. See this github issue.