我是Neo4J的新手。我已经构建了一个使用spring-data-neo4j(4.0.0.BUILD-SNAPSHOT - 版本),spring-boot(1.2.3.RELEASE - version)的项目,并成功创建节点实体,向节点实体添加属性并添加关系。它工作正常。现在我想为关系创建属性。我使用sdn4大学作为参考,这里是链接https://github.com/neo4j-examples/sdn4-university。
我想创建一个名为" challengedBy"对于关系PLAY_MATCH(开始节点是匹配,结束节点是播放器)。你可以看看下面的课程。
@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.
}
答案 0 :(得分:0)
我认为你在玩家终端节点中也有游戏匹配关系。如果您在播放器节点中注释以下代码。它应该工作。我还附加了一个json示例来从匹配URL(/ api / match)中的UI传递而不是(/ 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}}
}
这应该与属性challengedBy创建一个新匹配和一个新关系到一个id为732的现有玩家节点。
检查一下,如果有效,请告诉我。