0

我是 Neo4J 的新手。我已经建立了一个使用 spring-data-neo4j (4.0.0.BUILD-SNAPSHOT - 版本),spring-boot (1.2.3.RELEASE - 版本)的项目并成功创建节点实体,向节点实体添加属性并添加关系。它工作正常。现在我想为关系创建属性。我使用了 sdn4 大学作为参考,这里是链接https://github.com/neo4j-examples/sdn4-university

我想为关系 PLAY_MATCH 创建一个名为“challengedBy”的属性(开始节点是匹配,结束节点是播放器)。你可以看看下面的课。

@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;

}

我在项目 /api/playmatch 中创建了一个控制器,仅创建匹配和玩家之间的关系。因此,当我传递现有匹配节点和玩家节点的值时,根本不会创建关系。

任何帮助将不胜感激..

PlayMatch 代码是

@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.
}

匹配码是

@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.

}

播放器代码是

@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.
}
4

1 回答 1

0

我认为您在玩家端节点内也有比赛关系。如果您在播放器节点中注释以下代码。它应该工作。我还附加了一个 json 示例以从 UI 中的匹配 URL (/api/match) 而不是 (/api/playmatch) 传递

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

public PlayMatch getPlayMatch() {
    return playMatch;
}

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

示例 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}}
}

这应该创建一个新的匹配和一个新的关系,属性challengeBy 到一个现有的玩家节点,ID 为732。

检查一下,让我知道这是否有效。

于 2015-07-03T13:37:49.277 回答