私はSpring-Data-Neo4jを使ってSpring-Boot Projectを持っています。私はサービスクラスを使って関係をマップする方法を理解できません。Spring-Data-Neo4jノードをサービスクラスにマッピングする方法は?
私が作成したAPIは、シリーズゲームオブトロンズをテーマにしています。 王国を建造することはできますが、各城は多くの城を持つことができますが、各城は1つの王国を許されています。
プロジェクトはGitHubの上で、最新のコードを見つけるために、Devのブランチを確認してください: https://github.com/darwin757/IceAndFire
質問:
私は私の王国POJOを持っている、と私はそれその関係を追加しました城のリストがあります:
package com.example.Westeros.Kingdoms;
import java.util.ArrayList;
import java.util.List;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import com.example.Westeros.Castles.Castle;
@NodeEntity
public class Kingdom {
@GraphId private Long id;
private String name;
@Relationship(type = "Has a")
private List<Castle> castles = new ArrayList<Castle>();
public Kingdom() {}
public Kingdom(String name) {
super();
this.name = name;
}
//GETERS AND SETTERS FOR NAME
public List<Castle> getCastles() {
return castles;
}
public void addCastle(Castle castle) {
this.castles.add(castle);
}
}
をそして私は城と同じことをやった:
package com.example.Westeros.Castles;
import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import com.example.Westeros.Kingdoms.Kingdom;
@NodeEntity
public class Castle {
@GraphId
private Long id;
private String name;
@Relationship(type = "belongs to")
private Kingdom kingdom;
public Castle() {}
public Castle(String name) {
super();
this.name = name;
}
//GETTERS AND SETTERS FOR NAME
王国を追加できるようにするためには何を書いておかなければなりません。関連する城をデータベースに追加するにはどうすればよいですか?
これまでのところ、私はこの間違った方法があります:私は、このテストに合格する方法
@Test
public void addCastleToKingdomTest() {
kingdomService.addKingdom(theNorth);
kingdomService.addCastleToKingdom("The North", "Winterfell");
kingdomService.addCastleToKingdom("The North", "The Dreadfort");
kingdomService.addCastleToKingdom("The North", "White Harbor");
Assert.assertEquals("Winterfell", kingdomService.getKingdomsCastles("The North").get(0).getName());
Assert.assertEquals("The Dreadfort", kingdomService.getKingdomsCastles("The North").get(1).getName());
Assert.assertEquals("White Harbor", kingdomService.getKingdomsCastles("The North").get(2).getName());
}
は良い先生ありがとう:) – Darwin