Spring-Data-Neo4j如何通过服务映射节点class?

Spring-Data-Neo4j How to map nodes through the service class?

我有一个 Spring-使用 Spring-Data-Neo4j 的引导项目,但我不知道如何使用我的服务 class.

我建造的 API 以权力的游戏系列为主题。 你可以建造王国和城堡(到目前为止)每个王国可以有很多城堡,但每个城堡允许一个王国。

项目在 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);
}

}

我对 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

}

现在我必须在服务中写些什么才能将王国及其相关城堡添加到数据库中?

到目前为止我有这个错误的方法:

    //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());

}

addCastleToKingdomTest 向王国添加了一座城堡,但不会保留修改。

这解决了问题:

public void addCastleToKingdom(String kingdomName,String castleName) {
    Castle castle = new Castle(castleName);
    castleRepository.save(castle);
    Kingdom kingdom = getKingdom(kingdomName);
    kingdom.addCastle(castle);      
    kingdomRepository.save(kingdom);
}   

甚至更好,因为对象图由 SDN 传递持久化:

public void addCastleToKingdom(String kingdomName,String castleName) {
    Kingdom kingdom = getKingdom(kingdomName);
    Castle castle = new Castle(castleName);
    kingdom.addCastle(castle);
    kingdomRepository.save(kingdom);
}