Graph 如何在Orient DB中创建关系?

Graph 如何在Orient DB中创建关系?,graph,relationships,graph-databases,orientdb,Graph,Relationships,Graph Databases,Orientdb,我在Orient DB的人际关系中遇到了麻烦 我的问题是我不知道如何在orient DB中建立关系 我读了文档,看到它有命令创建链接。但我不知道当我插入新记录并且不运行create link命令时,它如何创建链接。仅当您从RDBMS导入并且它将外键转换为链接时,才创建链接。要创建链接,只需将目标的RID放入源对象。你在使用什么API?我有一个简单的例子 我有两张桌子。张贴和评论 表柱: +----+----------------+ | id | title | +----+-

我在Orient DB的人际关系中遇到了麻烦

我的问题是我不知道如何在orient DB中建立关系


我读了文档,看到它有命令创建链接。但我不知道当我插入新记录并且不运行create link命令时,它如何创建链接。

仅当您从RDBMS导入并且它将外键转换为链接时,才创建链接。要创建链接,只需将目标的RID放入源对象。你在使用什么API?

我有一个简单的例子

我有两张桌子。张贴和评论

表柱:

+----+----------------+
| id | title          |
+----+----------------+
| 10 | NoSQL movement |
| 20 | New OrientDB   |
+----+----------------+
+----+----------------++----------------+
| id | title          | comments
+----+----------------++----------------+
| 10 | NoSQL movement |[#7:0,#7:1,#7:2]
| 20 | New OrientDB   |[#7:3,#7:4]
+----+----------------++----------------+
表注释:

+----+--------+--------------+
| id | postId | text         |
+----+--------+--------------+
|  0 |   10   | First        |
|  1 |   10   | Second       |
| 21 |   10   | Another      |
| 41 |   20   | First again  |
| 82 |   20   | Second Again |
+----+--------+--------------+
所以现在。我运行命令:

 CREATE LINK comments TYPE linkset FROM comment.postId To post.id INVERSE.
INSERT INTO COMMENT (id, postId, text) VALUES( 5, 10, 'Six' );
表中的post将更改为:

表柱:

+----+----------------+
| id | title          |
+----+----------------+
| 10 | NoSQL movement |
| 20 | New OrientDB   |
+----+----------------+
+----+----------------++----------------+
| id | title          | comments
+----+----------------++----------------+
| 10 | NoSQL movement |[#7:0,#7:1,#7:2]
| 20 | New OrientDB   |[#7:3,#7:4]
+----+----------------++----------------+
所以现在。我想在注释表中插入一条记录

我运行以下命令:

 CREATE LINK comments TYPE linkset FROM comment.postId To post.id INVERSE.
INSERT INTO COMMENT (id, postId, text) VALUES( 5, 10, 'Six' );
我的问题是我不知道如何将表更新到:

  +----+----------------++----------------+
    | id | title          | comments
    +----+----------------++----------------+
    | 10 | NoSQL movement |[#7:0,#7:1,#7:2,#7:5]
    | 20 | New OrientDB   |[#7:3,#7:4]
    +----+----------------++----------------+
谢谢

你应该用这个

begin
let a=INSERT INTO COMMENT (id, postId, text) VALUES( 5, 10, 'Six' );
let b=update post add children=$s where @rid=YOUR_POST_RID/ID
commit
return $b;

/*****update#13:0 add children=#12:1您也可以这样做,其中#13:0是post id,而#12:1是comment id***************/

我正在使用Java API,并说明维护文档之间关系的最佳实践是什么。现在我是这样做的:

protected static ODocument addReference(ODocument d1, ODocument d2) {
        Collection<ODocument> ref = d1.field(FIELD_ID, OType.LINKSET);
        if(ref == null) {
            ref = new HashSet<>();
        }
        ref = new HashSet<>(ref);
        ref.add(d2);
        return d1.field(FIELD_ID, ref, OType.LINKSET).save();
    }
}
受保护的静态气味文件addReference(气味文件d1、气味文件d2){
集合引用=d1.字段(字段ID,OType.链接集);
如果(ref==null){
ref=新的HashSet();
}
ref=新哈希集(ref);
参考附录(d2);
返回d1.field(field_ID,ref,OType.LINKSET).save();
}
}

但是我经常遇到NPE或ClassCastException,所以我怀疑这是正确的方法。

现在我正在使用这个类,到目前为止,它的工作方式与预期的一样

公共类文档引用{
私有最终静态记录器=Logger.getLogger(DocumentsReferences.class);
私有静态void addToReferenceCollection(ODocument orid、字符串类名、字符串字段名、orid值){
Collection refCollection=newhashset(getReferenceCollection(orid,className,fieldName));
refCollection.add(值);
setReferenceCollection(orid、类名、字段名、引用集合);
}
公共静态void建立many2manyrelationship(ODocumentDocumentFirst,String classNameFirst,
String fieldNameFirst、OdocumentDocumentSecond、String classNameSecond、String fieldNameSecond){
//左侧
addToReferenceCollection(documentFirst、classNameFirst、fieldNameFirst、documentSecond.getIdentity());
//右侧
addToReferenceCollection(documentSecond、classNameSecond、fieldNameSecond、documentFirst.getIdentity());
}
公共静态void建立many2onerelationship(ODocumentDocumentFirst,String classNameFirst,
String fieldNameFirst、OdocumentDocumentSecond、String classNameSecond、String fieldNameSecond){
//左侧
setReference(documentFirst、classNameFirst、fieldNameFirst、documentSecond);
//右侧
addToReferenceCollection(documentSecond、classNameSecond、fieldNameSecond、documentFirst.getIdentity());
}
public static void buildingOne2ManyRelationship(ODocument documentFirst、String classNameFirst、,
String fieldNameFirst、OdocumentDocumentSecond、String classNameSecond、String fieldNameSecond){
//左侧
addToReferenceCollection(documentFirst、classNameFirst、fieldNameFirst、documentSecond.getIdentity());
//右侧
setReference(documentSecond、classNameSecond、fieldNameSecond、documentFirst);
}
public static void buildione2onerelationship(文档文档优先,字符串类名称优先,
String fieldNameFirst、OdocumentDocumentSecond、String classNameSecond、String fieldNameSecond){
//左侧
setReference(documentFirst、classNameFirst、fieldNameFirst、documentSecond);
//右侧
setReference(documentSecond、classNameSecond、fieldNameSecond、documentFirst);
}
公共静态ORID getReference(ODocument文档、字符串类名、字符串字段名){
if(isValid(文档、类名)){
OIdentifiable result=document.field(fieldName,ottype.LINK);
如果(结果==null){
返回null;
}
返回result.getIdentity();
}
抛出新的IllegalArgumentException(“For”+文档);
}
公共静态集合getReferenceCollection(ODocument文档、字符串类名、字符串字段名){
if(isValid(文档、类名)){
试一试{
收集结果=document.field(fieldName,OType.LINKSET);
如果(结果==null){
返回集合;
}
返回result.stream().map(可识别->可识别.getIdentity())
.collect(收集器.toCollection(ArrayList::new));
}捕获(例外e){
logger.error(e.getLocalizedMessage(),e);
返回集合;
}
}
抛出新的IllegalArgumentException(“For”+文档);
}
公共静态布尔值有效(ODocument文档,字符串类名称){
返回文档。isValid(文档,类名);
}
/**
*@return{@link-ORID-ORIDs}所有文档的{@link-Collection}
*已在此操作期间删除
*/
public static Collection releaseManyManyManyRelationship(ODocumentDocumentFirst,String ClassName First,

String fieldNameFirst,CollectionHi Lvca!我的示例遵循此页,当我想插入一条记录以注释表一条新记录时。我的问题是我不知道如何更新post表的字段注释?谢谢!对不起,我不明白,您能详细说明一下吗?为什么您要将您的问题作为答案发布是的,我也想知道这一点.据我所知,对于文档api,文档从未解释过如何实际添加关系。