Querying a Relationship in Spring Data Neo4j

Hello Guys,
Recently started using Neo4j for my final year project, ran in to a little problem while doing so. I'm maintining a Beacon network where there is only one Node Entity and many relationships between them. I want to know that Am I doing this right. When I queried and Relationship , it did not work. Link to the question is below. Help is greatly appreciated.

Do you literally have MACs like f:f:f:f, or were you expecting f to be replaced, somehow?

1 Like

Thanks for replying. I was going to replace them in the future, but for testing purposes , I created the database like this,

{
"description": "blah_blah",
"location": "stair1",
"MAC": "b:b:b:b"
}

Understood. Well, that query uses f:f:f:f for matching both b AND c, so unless that node is ADJACENT to itself, you wouldn't match.

1 Like

Oh it seems I pasted the wrong code in to StackOverflow, In this case c has a different MAC. (e.g - "r:r:r:r"). { changed it in the original post, thanks for pointing it out :) )

When I queried in the Neo4j browser , it returns the Relationship correctly,

{
"angle": 180,
"cost": 4
}

But it seems I could not retrieve that data with my current class structure.

Does it make any difference if you return *?

1 Like

Yes then the API returns this,
{
"angle": 180,
"cost": 4,
"startBeacon": {
"description": "blah_blah",
"location": "junction1",
"mac": "f:f:f:f"
},
"endBeacon": {
"description": "rsad",
"location": "junction2",
"mac": "r:r:r:r"
}
}

This kind of result is more than enough for me , thank you for pointing it out. Can I know the issue with the previous method ?

When you run a query in the Neo4j browser, it is "autocompleting" it for you, meaning it expands the returned entity. In this case, you were returning only the relationship between Beacons. The * ensures the object graph mapper (OGM) sees the entire path, not just the connection. I'm not sure if this is the only way to accomplish this, but this has been successful for me.

1 Like

Understood , thank you very much. Is that the normal way to retrieve a relationship in Neo4j ? I saw some posts about using @QueryResult class as well.

I think the @QueryResult is kind of a way to hold an interim result, a DTO you'd use when you want to return not just a object-mapped entity, but also some kind of calculated data (or other entities).

If you have very complex queries with lots of transient variables in play, it is most efficient to only return the ones you need - that's a case you might reconsider the catch-all *. In this example, you could return a, b, c and have the same result.

If you want to go deeper, you can capture a property on the relationship, by defining e.g.:

@RelationshipEntity(type = "ADJACENT")
data class ThisEntityToThatEntity(@Id @GeneratedValue var id: Long? = null,
                                 @Property(name = "myProp") var myProp: String? = null,
                                 @StartNode var e1: ThisEntity? = null,
                                 @EndNode var e2: ThatEntity? = null)

And

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

Understood, thank you for taking your time to reply , really appreciate it. Finally can you tell me some good resources to refer other than the official docs ? Like example projects and as such ? Because I had a hard time finding resources other than the docs.

It can be tough, I use this forum pretty frequently. The primary docs tell much of the story, but don't forget to check out the SDN docs. If you cannot get a question answered, don't hesitate to @mention someone you can tell from other questions is familiar with a certain topic.

1 Like

Will do . Thanks again man.

Glad I could help, Ravindu.

Could you mark my third response as the solution, and also on SO?

1 Like

With pleasure :slight_smile: