Spring-Data-Neo4j를 사용하는 Spring-Boot Project가 있고 서비스 클래스를 사용하여 내 관계를 매핑하는 방법을 알 수 없습니다.Spring-Data-Neo4j 서비스 클래스를 통해 노드를 매핑하는 방법은 무엇입니까?
내가 작성한 API는 일련의 게임 중 하나입니다. 당신은 왕국을 건설 할 수 있습니다, 그리고 성 (지금까지) 각 왕국은 많은 성을 가질 수 있지만 각 성은 하나의 왕국이 허용됩니다.
프로젝트는 GitHub의에 최신 코드를 찾기 위해 데브 지점을 확인해야합니다 : https://github.com/darwin757/IceAndFire
질문 :
내가 내 나라 뽀조를 가지고 있고, 내가 그 관계를 추가했습니다 성 목록이 있습니다
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
,536,
이제 왕국을 추가하고 관련 성을 데이터베이스에 추가하려면 무엇을 써야합니까?
지금까지 나는이 잘못된 방법이 있습니다
//FIXME I'M WRONG
public void addCastleToKingdom(String kingdomName,String castleName) {
Castle castle = new Castle(castleName);
castleRepository.save(castle);
getKingdom(kingdomName).addCastle(castle);
내가이 시험
@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