SDN 4 doesn't create relationship with properties

241 views Asked by At

I am new to Neo4J. I have built a project that uses spring-data-neo4j (4.0.0.BUILD-SNAPSHOT - version), spring-boot (1.2.3.RELEASE - version) and succeeded to create node entities, add properties to node entities and add relationships. It works fine. Now I want to create properties for the relationships. I have used sdn4 university as a reference, here is the link https://github.com/neo4j-examples/sdn4-university .

I want to create a property called "challengedBy" for relationship PLAY_MATCH (Start node is Match and end node is Player). You can have a look on below class.

@RelationshipEntity(type = "PLAY_MATCH")
public class PlayMatch extends Entity {
//Entity is a class with the id property for the node / relationship 
@Property
private String challengedBy;

@StartNode
private Match match;

@EndNode
private Player player1;

}

I have created a controller in the project /api/playmatch to create only the relationship between match and a player. So when I pass the values for an existing match node and a player node, the relationship is not created at all.

Any help will be appreciated..

PlayMatch code is

@RelationshipEntity(type = "PLAY_MATCH")
public class PlayMatch extends Entity{

@Property
private String challengedBy;

@StartNode
private Match match;

@EndNode
private Player player1;

public PlayMatch() {

}
public PlayMatch(String challengedBy, Match match,
        Player player1) {
    super();
    this.challengedBy = challengedBy;
    this.match = match;
    this.player1 = player1;
}

// after this i have getters  & setters and toString method for above fields.
}

Match code is

@NodeEntity(label = "Match")
public class Match extends Entity {

private String createdBy;
private Long createdTime;
private String status;
private int noOfGames;
private int noOfPoints;
private String type;
private Long date;

@Relationship(type="PLAY_MATCH",direction= Relationship.UNDIRECTED)
private PlayMatch playMatch;

public Match() {

}   

public Match(String createdBy, Long createdTime, String status,
    int noOfGames, int noOfPoints, String type, Long date) {
    super();
    this.createdBy = createdBy;
    this.createdTime = createdTime;
    this.status = status;
    this.noOfGames = noOfGames;
    this.noOfPoints = noOfPoints;
    this.type = type;
    this.date = date;
}

public PlayMatch getPlayMatch() {
    return playMatch;
}

public void setPlayMatch(PlayMatch playMatch) {
    this.playMatch = playMatch;
}
// after this i have getters  & setters and toString method for above fields.

}

Player code is

@NodeEntity(label = "Player")
public class Player extends Entity {

private String address;
private String preferredSport;
private float height;
private float weight;
private String phone;
private String photo;

@Relationship(type="PLAY_MATCH")
private PlayMatch playMatch;

public PlayMatch getPlayMatch() {
    return playMatch;
}

public void setPlayMatch(PlayMatch playMatch) {
    this.playMatch = playMatch;
}

public Player() {
}

public Player(String address, String preferredSport, float height,
    float weight, String phone, String photo) {
    super();
    this.address = address;
    this.preferredSport = preferredSport;
    this.height = height;
    this.weight = weight;
    this.phone = phone;
    this.photo = photo;
}

// after this i have getters  & setters and toString method for above fields.
}
1

There are 1 answers

2
Ganesh Babu On BEST ANSWER

I think you have playmatch relationship within the player end node as well. If you comment the following code in the player node. It should work. I have also attached a json sample to pass from the UI in the match URL (/api/match) instead of (/api/playmatch)

@Relationship(type="PLAY_MATCH")
private PlayMatch playMatch;

public PlayMatch getPlayMatch() {
    return playMatch;
}

public void setPlayMatch(PlayMatch playMatch) {
    this.playMatch = playMatch;
}

Sample JSON

{
    "type": "typename",
    "status": "statusname",
  "createdTime": 1435928223021,
    "noOfGames": 5,
    "noOfPoints": 19,
  "playMatch": {"challengedBy" : "John", "player1" : {"id":732}, "match":{"type": "typename",
    "status": "statusname",
  "createdTime": 1435928223021,
    "noOfGames": 5,
    "noOfPoints": 19}}
}

this should create a new match and a new relationship with property challengedBy to an existing player node with id 732.

check it out and let me know if this works.