尝试将顶点连接到父顶点。父顶点是第一次创建的,子顶点是链接到它的。
示例:
// Setup
OrientDB db = new OrientDB("remote:localhost:2424", "admin", "admin", OrientDBConfig.defaultConfig());
if(db.exists("temp")){
db.drop("temp");
}
db.createIfNotExists("temp", ODatabaseType.PLOCAL);
ODatabaseSession session = db.open("temp", "admin", "admin");
session.createClassIfNotExist("VBase", OClass.VERTEX_CLASS_NAME);
session.createClassIfNotExist("VTest", OClass.VERTEX_CLASS_NAME);
session.createClassIfNotExist("ETest", OClass.EDGE_CLASS_NAME);
// Phase 1
session.begin();
OVertex parent = session.newVertex("VBase");
parent.save();
Map<String, Object> params = new HashMap<>();
params.put("ParentRID", parent.getIdentity());
OVertex test = session.execute("sql",
"LET $a = CREATE VERTEX VTest SET name = 'John';" +
"CREATE EDGE ETest FROM :ParentRID TO $a;" +
"RETURN $a;", params)
.vertexStream().findFirst().orElse(null);
assert test != null;
session.commit();
// Phase 2
session.begin();
parent = session.execute("sql", "SELECT FROM VBase LIMIT 1;",new HashMap<>())
.vertexStream().findFirst().orElse(null);
params = new HashMap<>();
params.put("ParentRID", parent.getIdentity());
test = session.execute("sql",
"LET $a = CREATE VERTEX VTest SET name = 'John';" +
"CREATE EDGE ETest FROM :ParentRID TO $a;" +
"RETURN $a;", params)
.vertexStream().findFirst().orElse(null);
assert test != null;
session.commit();
// Done
System.out.println(test.getIdentity());
但是,在工作室中搜索显示第二个顶点未连接:
MATCH {class:VBase}.out() {class:VTest, as:vt} RETURN vt;
仅返回1个结果。该图就像: