0

Spent multiple days trying to figure out why this isn't working. My model is Player-[:PLAYED_WITH_TEAM]->team-[:CONTESTED_IN]->league. Few instances of this relation is as follows

bob-[:PLAYED_WITH_TEAM]->falcons-[:CONTESTED_IN]->ABC League
alice-[:PLAYED_WITH_TEAM]->falcons-[:CONTESTED_IN]->ABC League
bob-[:PLAYED_WITH_TEAM]->falcons-[:CONTESTED_IN]->XYZLeague

Bob played for the same team "Falcons" in two leagues ABC and XYZ. This is the fact i want to capture. Since Bob played for the same team in 2 different leagues, I need to have two PLAYED_WITH_TEAM relations between the same start (Bob) and end (Falcons) nodes.

I am using spring data and have the entities defined. I am able to create 2 such relationships but not more than two using spring data. i.e. if bob played for the same team Falcons for another 3rd league, I am unable to create that 3rd relation. I am not sure where the problem is. Below is my code for creating the new relation. PlayedWith is a RelationshipEntity with Player as the start node and Team as the end node.

private PlayedWith createPlayedWithRelation(League currentLeague, Team team, Player p)
    {
        System.err.println("Creating PLAYED_WITH_TEAM relation between " + team + " and " + p + " and " + currentLeague);

        PlayedWith playedWith = template.createRelationshipBetween(p, team, PlayedWith.class, "PLAYED_WITH_TEAM", true);
        playedWith.setDuring(currentLeague.getStartDate());
        playedWith.setInLeague(currentLeague);
        playedWith.setPlayer(p);
        playedWith.setTeam(team);
        playedWith.setAsCaptain(p.isCaptain());

        team.addPlayer(p);
        template.save(playedWith);

        return playedWith;
    }

PlayedWith

@RelationshipEntity (type = "PLAYED_WITH_TEAM")
public class PlayedWith
{
    @GraphId
    private Long nodeId;

    @StartNode
    Player player;

    @Fetch
    @EndNode
    Team team;
}

Let me know if there is an alternate way of storing this scenario.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

2 Answers2

0

You do not need to add another relationship between bob and the falcons but between the falcons and the new league like this:

(falcons)-[:CONTESTED_IN]->(NEWLeague)

As bob plays for the falcons and the falcons then contested in ABC League, XYZ League and NEW League bob implicitly plays in those three leagues.

Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
0

Actually there should only be one :PLAYED_WITH_TEAM relationship between bob and the falcons. Are you sure that you query is right for getting the amount of :PLAYED_WITH_TEAM relationships between Bob and the falcons?

From the SDN reference documentation:

Note

Spring Data Neo4j ensures by default that there is only one relationship of a given type between any two given entities. This can be circumvented by using the createRelationshipBetween() method with the allowDuplicates parameter on repositories or entities.

PhilBa
  • 732
  • 4
  • 16