rashm1n
rashm1n

Reputation: 85

Querying a Relationship in Spring Data Neo4j

I'm designing a Beacon Network, Where beacons are adjacent to each other, using Neo4j database where it has One Entity and One Relationship class. I need to retrieve a Relationship between two Beacons but cant figure out how. Here are the two classes

The Beacon Class

public class Beacon {
    @Id
    private String MAC;
    private String description;
    private String location;

    @Relationship(type = "ADJACENT")
    private List<Adjacent> adjacentList = new ArrayList<>();

    public Beacon() {
    }

    public Beacon(String MAC, String description, String location) {
        this.MAC = MAC;
        this.description = description;
        this.location = location;
    }


    public void addAdjacency(Adjacent adjacent){
        if (this.adjacentList==null){
            this.adjacentList=new ArrayList<>();
        }
        this.adjacentList.add(adjacent);
    }

//Getters and Setters are excluded

}

The Adjacent Relationship Class

public class Adjacent {
    @Id
    @GeneratedValue
    private Long id;

    private int angle;
    private int cost;

    @StartNode
    private Beacon startBeacon;

    @EndNode
    private Beacon endBeacon;

    public Adjacent() {
    }

    public Adjacent(int angle, int cost, Beacon startBeacon, Beacon endBeacon) {
        this.angle = angle;
        this.cost = cost;
        this.startBeacon = startBeacon;
        this.endBeacon = endBeacon;
    }
//Getters and Setters are excluded

}

I already tried creating a Repository and retrieving, but even though the query works in Neo4j Browser, it does not retrieve any data here, just blank parenthesis.

public interface AdjacentRepository extends Neo4jRepository<Adjacent,Long> 
{
@Query("match (b:Beacon{MAC:\"f:f:f:f\"})-[a:ADJACENT]-(c:Beacon{MAC:\"r:r:r:r\") return a")
    Adjacent findaRelationshipp();
}

Any help is greatly appreciated.

Upvotes: 1

Views: 540

Answers (1)

Jiropole
Jiropole

Reputation: 174

You'll need to return *, or return a, b, c so the OGM can infer all the details necessary to map the query response to your object model.

The reason the query worked in the Neo4j Browser is because it automatically modifies your query to expand the adjacent path, in this case, the Beacon objects.

Upvotes: 2

Related Questions