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

时间:2015-04-08 09:25:47

标签: java orientdb

假设我有一个顶点(我们称之为“PokemonMaster”)

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

在我的数据库中包含一个EMBEDDEDMAP(我也尝试过LINKMAP但我不确定我在做什么)的“口袋妖怪”这个类。

我正在尝试使用Java来创建Vertex并在“pokemons”字段中添加一些pokemons。

让我们说口袋妖怪看起来像:

Pokemon
{
  name, (STRING)
}

我正在做类似的事情:

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

我认为这会在地图中创建第一个元素(皮卡丘)。我希望稍后可以通过做类似的事情将一些Pokemons添加到我的地图中:

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

希望它会帮助别人!

1 个答案:

答案 0 :(得分:1)

<强>更新

如果是embeddedmap,则创建架构:

    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