2

I have a model where User will have a list of Roles and Role has a list of Permissions. However, even when i save all of them at once - with depth -1 I am unable to retrieve the child nodes from the parent nodes. ex: user.getRoles() - 2 [role1,role2] role1.getAssociatedFeature() - 0 But if i get the Role from the DB Ex : findByRoleName('role1') -> [Role: role1,Display Role,associatedFeatures[2]]

User.java

@NodeEntity
public class User {

@GraphId Long id;
private String name;
private String loginUserName;

@Relationship(type="ROLE")
private Set<Role> associatedRoles = new HashSet<Role>();

} Role.java

@NodeEntity
public class Role {

    @GraphId Long id;
    private String roleName;
    private String displayRoleName;
    @Relationship(type="ACCESS_TO")
    private Set<Feature> associatedFeatures = new HashSet<Feature>();
}
Feature.java
@NodeEntity
public class Feature {
   @GraphId Long id;
   private String featureName;
   @Relationship(type="HAS_PERMISSION") 
   private Set<Permission> permissions = new HashSet<Permission>();
}
@NodeEntity
public @Data class Permission {
  @GraphId
  Long id;
  String permission;
}

I am using Spring data jpa to use the CRUD operations: <>Repository.java - This will bydefault implement save,update,delete,find

@RepositoryRestResource()
public interface RoleRepository extends GraphRepository<Role>{...}

ServiceImpl.java
    @Override
        public User create(User u) {
            return userRepo.save(u,-1);
        }

In my Junit- I am creating a new User entity, and populating the data all the way to permission. But when i fetch the user -> i only get the roles but not the features, permission along the chain.

In the neo4j DB browser, I see that all the nodes are created with appropriate dependency. Any pointers on how to save and traverse through the graph?

Swetha V
  • 300
  • 5
  • 16

1 Answers1

3

The default load depth is 1. This means you'll get the user and the associated roles, but not the role's features or anything deeper in the graph.

You can specify the load depth if the default is not what you want:

userRepo.findOne(user.getId(), 3);

http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#_fine_grained_control_via_depth_specification

Vince
  • 2,181
  • 13
  • 16
  • 1
    Thanks Vince!! That worked. I was saving with depth "-1" i.e save everything - but while fetching i was using a custom find (findByName) and not the one defined in GraphRepository and that was using the default load depth to fetch. I do not have enough points to upvote an answer, so a verbal Thanks for now :) – Swetha V Mar 19 '16 at 23:43
  • No problem. Just FYI, there's no need to specify -1 on save depth - it is the default depth for save: everything reachable from the top level object you're persisting. – Vince Mar 20 '16 at 17:25