2015-06-29 41 views
0

我是Neo4J的新手。我已經構建了一個使用spring-data-neo4j(4.0.0.BUILD-SNAPSHOT-版本),spring-boot(1.2.3.RELEASE-版本)併成功創建節點實體的項目,爲節點實體添加屬性並添加關係。它工作正常。現在我想創建關係的屬性。我用sdn4大學作爲參考,這裏是鏈接https://github.com/neo4j-examples/sdn4-universitySDN 4不會與屬性建立關係

我想爲關係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. 
} 
+0

你是如何創建PlayMatch的,你能分享代碼嗎? Match和Player也是什麼樣子? – Luanne

+0

@Luanne我已更新帖子,請看看它,並讓我知道更新,在此先感謝 – Aditya

+0

看起來很好,這是什麼問題?您是否在保存之前在播放器上設置了PlayMatch並匹配? – Luanne

回答

0

我認爲你有playmatch RELAT在玩家端節點內的離子也是如此。如果您在播放器節點中註釋以下代碼。它應該工作。我還附上一個JSON樣品從在匹配URL(/ API /匹配)的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的現有球員節點。

檢查出來,讓我知道這是否工作。

+0

讓我檢查並回復你,謝謝你的回覆。 – Aditya

+0

謝謝@Ganesh Babu ..現在工作正常,我能夠創建與屬性的關係。 – Aditya