Reputation: 369
I have a two-way [:FRIENDSHIP] relationship between User nodes:
(UserA)<-[:FRIENDSHIP {approved:true}]->(UserB)
Here's a little test function in Java to setup the relationship:
public void approveFriendship(User requestor, User requestee) {
Friendship friendship = new Friendship(requestor, requestee);
friendship.setApproved(true);
Friendship invertedFriendship = new Friendship(requestee, requestor);
invertedFriendship.setApproved(true);
requestor.getFriendships().add(friendship);
requestee.getFriendships().add(invertedFriendship);
userRepository.save(requestor);
userRepository.save(requestee);
}
This cypher query returns the friends of UserA, and works fine:
start user=node({0})
match (user)-[r?:FRIENDSHIP]->(friends)
where r.approved = true
return friends
This cypher query returns the posts of a friend, and does not work (returns empty result):
start n=node({0})
match (n)<-[r?:FRIENDSHIP]->(friend)<-[:AUTHOR]-(friendposts)
where r.approved = true
return friendposts order by friendposts.createdAt
When omitting the where r.approved = true
line or changing it to where r.approved = false
it returns posts of friends without the approved status in both cases.
Can anybody spot if I'm doing something wrong here? Much obliged.
Upvotes: 0
Views: 1389
Reputation: 369
Solved it.
Dunno why, but in a one-to-many type relationship (eg. User to Post) Neo4J prefers outgoing relationships on the Collection, and the incoming on the single entity. I had it the other way round...
Now my classes look like this:
public class User {
@RelatedTo(type = "AUTHOR")
private Set<Post> posts;
}
public class POST {
@RelatedTo(type = "AUTHOR", direction = Direction.INCOMING)
private User author;
}
And of course the cypher query has to change to reflect the new relationship direction (note arrow behind author):
start n=node({0})
match (n)<-[r?:FRIENDSHIP]->(friend)-[:AUTHOR]->(friendposts)
where r.approved = true
return friendposts order by friendposts.createdAt
Upvotes: 2