如何使用 Java 在 Vertex 的地图字段中保存 ODocument

How to save ODocument in map field in Vertex with Java

假设我有一个顶点(我们称它为 "PokemonMaster")

PokemonMaster
{
 name, (STRING)
 age, (INTEGER)
 pokemons, (EMBEDDEDMAP) of Pokemon
}

在我的数据库中包含 class "Pokemon".

的 EMBEDDEDMAP(我也尝试使用 LINKMAP,但我不确定我在做什么)

我正在尝试使用 Java 创建顶点并放入字段 "pokemons",一些小精灵。

假设一个口袋妖怪看起来像:

Pokemon
{
  name, (STRING)
}

我正在做类似的事情:

Vertex v = graph.addVertex("class:PokemonMaster",
                           "name", "Sacha",
                           "age", "42",
                           "pokemons", new ODocument("Pokemon").field("name", "Pikachu")); 

我假设这会在地图中创建第一个元素(皮卡丘)。我希望以后能够通过执行以下操作将一些口袋妖怪添加到我的地图中:

v.setProperty("pokemons", new ODocument("Pokemon").field("name", "Raichu"));

所有这些实际上都不起作用,这就是我来这里的原因,我完全错了吗?

我收到错误:

The field 'PokemonMaster.pokemons' has been declared as EMBEDDEDMAP but an incompatible type is used. Value: Pokemon{name:Pikachu}

谢谢!

编辑

我找到了解决办法。 创建像这样的地图:

Map<String, ODocument> foo = new HashMap<>();

在里面放一些小精灵:

ODocument doc = new ODocument("Pokemon").field("name", "Pikachu");
ODocument doc2 = new ODocument("Pokemon").field("name", "Raichu");
foo.put("pikachu", doc);
foo.put("raichu", doc2);
doc.save();
doc2.save();

并简单地将地图作为参数:

Vertex v = graph.addVertex("class:PokemonMaster",
                           "name", "Sacha",
                           "age", "42",
                           "pokemons", foo);

希望对大家有所帮助!

更新:

如果是嵌入式地图,要创建模式:

    OrientGraphNoTx graphOne = new OrientGraphNoTx(URL, USER, USER);
    try {
        OSchema schema = graphOne.getRawGraph().getMetadata().getSchema();

        OClass pokemon = schema.createClass("Pokemon");
        pokemon.createProperty("name", OType.STRING);

        OClass vClass = schema.getClass("V");
        OClass pokemonMaster = schema.createClass("PokemonMaster");
        pokemonMaster.setSuperClass(vClass);
        pokemonMaster.createProperty("name", OType.STRING);
        pokemonMaster.createProperty("age", OType.INTEGER);
        pokemonMaster.createProperty("pokemons", OType.EMBEDDEDMAP, pokemon);
    } finally {
        graphOne.shutdown();
    }

创建一个拥有神奇宝贝的大师:

    String pmRID = "";

    OrientGraph graphTwo = new OrientGraph(URL, USER, USER);
    try {
        ODocument pokemon = new ODocument("Pokemon");
        pokemon.field("name", "Pikachu");
        Map<String,ODocument> foo = new HashMap();
        foo.put("pikachu", pokemon);

        OrientVertex v = graphTwo.addVertex("class:PokemonMaster",
                "name", "Sacha",
                "age", "42",
                "pokemons", foo);

        graphTwo.commit();
        pmRID = v.getIdentity().toString();
    } catch (Exception e) {
        // ...
    } finally {
        graphTwo.shutdown();
    }

添加第二个神奇宝贝:

    OrientGraph graphThree = new OrientGraph(URL, USER, USER);
    try {
        ODocument pokemon = new ODocument("Pokemon");
        pokemon.field("name", "Raichu");

        OrientVertex v = graphThree.getVertex(pmRID);
        Map<String, ODocument> pokemons = v.getProperty("pokemons");
        if (pokemons == null) {
            pokemons = new HashMap();
        }
        pokemons.put("raichu", pokemon);
        v.setProperty("pokemons", pokemons);

        graphThree.commit();
    } catch (Exception e) {
        // ...
    } finally {
        graphThree.shutdown();
    }

您也可以使用嵌入式列表。参见 here