gpt4 book ai didi

java - 如何使用 Java 在 Vertex 的 map 字段中保存 ODocument

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:30 25 4
gpt4 key购买 nike

假设我有一个 Vertex(我们称它为“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"));

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

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 :

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

在里面放一些小 Sprite :

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

并简单地将 map 作为参数:

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

希望对大家有所帮助!

最佳答案

更新:

在嵌入式 map 的情况下,创建架构:

    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 .

关于java - 如何使用 Java 在 Vertex 的 map 字段中保存 ODocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29510776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com