DIVA
DIVA

Reputation: 579

How to fetch the relationship between two nodes using Neo4j with Spring boot GraphRepository?

I'm a beginner to Neo4j and i have the basic idea about neo4j graph model. I would like to know how to fetch the relationship between just two nodes with java and cyper query. I have two entities like following Users and Roles. I want to retrieve the role of the user. I have cyper query that's working fine. When i tried through neo4j entity classes i'm missing something. Please take a look of below code snippet,

User entity class:

public class Users extends Entity{

    /*private Long userId;*/
    private String name;
    private String email;
    private String login;
    private String password;

    @Relationship(type = "HAS_ROLE")
    Set<Roles> roles=new HashSet<Roles>();

    public Users(){
        roles = new HashSet<Roles>();
    }

    public Users(Users user) {
        super();
        this.name = user.getName();
        this.login = user.getLogin();
        this.password = user.getPassword();
        this.roles = user.getRoles();
    }

    public Set<Roles> getRoles() {
        return roles;
    }

    public void setRoles(Set<Roles> roles) {
        this.roles = roles;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Roles Entity class:

public class Roles extends Entity implements GrantedAuthority{

    private static final long serialVersionUID = 1L;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getAuthority() {
        return name;
    }
}



 public interface UserRepository extends GraphRepository<Users> {

     @Query("MATCH (user:Users)-[r]->(role:Roles) WHERE user.login = {0} RETURN user, role");  
     Users findByLogin(String login);

   }

Could anyone please help me to know to fetch the roles against the user ?

TIA..,

Upvotes: 1

Views: 1615

Answers (2)

Xavi Torrens
Xavi Torrens

Reputation: 347

It's important to know that for fetch a node with cypher and java, you must include the relationship between the first node and the null valued one in the RETURN clause:

@Query("MATCH (u:User)<-[rel:RELATIONSHIP]-(r:Roles) " +
            " RETURN u,r,rel")
    Users findByLogin(String login);

Without that, the Stay property in Reference will be null valued.

Upvotes: 2

Luanne
Luanne

Reputation: 19373

Derived finders work with a depth of 1, so you could do

 public interface UserRepository extends GraphRepository<Users> {
   Users findByLogin(String login);
 }

If you want to use a @Query, return a path (and adjust the depth as required). This example is the same as above- depth=1

public interface UserRepository extends GraphRepository<Users> {
   @Query("MATCH p=(user:Users)-[r*0..1]->() WHERE user.login = {0} RETURN p") 
   Users findByLogin(String login);
 }

Upvotes: 2

Related Questions