2013-05-02 74 views
0

我在东方数据库中遇到了问题。如何在Orient-DB中创建关系?

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

我读过文档,看到它有命令CREATE LINK。但我不知道它如何创建链接,当我插入新的记录,并不运行命令CREATE LINK。

回答

0

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

+0

嗨Lvca! 我做的例子关注这个页面https://code.google.com/p/orient/wiki/ImportFromRDBMS 而当我想插入一条记录来评论表中的新纪录。 我的问题是我不知道如何对帖子表的字段评论可以更新? 谢谢! – Hyeongsik 2013-05-02 10:52:56

+0

对不起,我还没有明白,你能说得更好吗? – Lvca 2013-05-02 11:23:05

0

我举个简单的例子简单

我有两个表。发布和评论。

表POST:

+----+----------------+ 
| id | title   | 
+----+----------------+ 
| 10 | NoSQL movement | 
| 20 | New OrientDB | 
+----+----------------+ 

表注释:

+----+--------+--------------+ 
| id | postId | text   | 
+----+--------+--------------+ 
| 0 | 10 | First  | 
| 1 | 10 | Second  | 
| 21 | 10 | Another  | 
| 41 | 20 | First again | 
| 82 | 20 | Second Again | 
+----+--------+--------------+ 

所以现在。所以,现在

+----+----------------++----------------+ 
| 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. 

在表后会变成:

TABLE POST。我想插入一条记录到评论表。

我运行此命令:

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] 
    +----+----------------++----------------+ 

谢谢!

+0

为什么你发布你的问题作为答案 – 2015-09-29 06:40:54

0

你应该使用这个

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; 

/*****更新#13:0儿童= 12:1你也可以做这样的地方#13:0是帖子的ID,而#12 :1是评论ID **********/

0

我正在使用Java API并且弄乱了维护文档之间关系的最佳做法。现在Im做这样的:

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

但我碰上NPE的ClassCastExceptions或相当频繁,所以我怀疑这是做正确的方式。

+0

是的,我也想知道这一点。据我所知,文档并没有解释,对于文档api,如何实际添加关系。 – 2016-12-12 15:16:14

0

现在我正在使用这个类,它按预期工作到目前为止。

public class DocumentsReferences { 

    private final static Logger logger = Logger.getLogger(DocumentsReferences.class); 

    private static void addToReferenceCollection(ODocument orid, String className, String fieldName, ORID value) { 

    Collection<ORID> refCollection = new HashSet<>(getReferenceCollection(orid, className, fieldName)); 
    refCollection.add(value); 
    setReferenceCollection(orid, className, fieldName, refCollection); 
    } 

    public static void establishMany2ManyRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    // left side 
    addToReferenceCollection(documentFirst, classNameFirst, fieldNameFirst, documentSecond.getIdentity()); 
    // right side 
    addToReferenceCollection(documentSecond, classNameSecond, fieldNameSecond, documentFirst.getIdentity()); 
    } 

    public static void establishMany2OneRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    // left side 
    setReference(documentFirst, classNameFirst, fieldNameFirst, documentSecond); 
    // right side 
    addToReferenceCollection(documentSecond, classNameSecond, fieldNameSecond, documentFirst.getIdentity()); 
    } 

    public static void establishOne2ManyRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    // left side 
    addToReferenceCollection(documentFirst, classNameFirst, fieldNameFirst, documentSecond.getIdentity()); 
    // right side 
    setReference(documentSecond, classNameSecond, fieldNameSecond, documentFirst); 
    } 

    public static void establishOne2OneRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    // left side 
    setReference(documentFirst, classNameFirst, fieldNameFirst, documentSecond); 
    // right side 
    setReference(documentSecond, classNameSecond, fieldNameSecond, documentFirst); 
    } 

    public static ORID getReference(ODocument document, String className, String fieldName) { 

    if (isValid(document, className)) { 
     OIdentifiable result = document.field(fieldName, OType.LINK); 
     if (result == null) { 
     return null; 
     } 
     return result.getIdentity(); 
    } 
    throw new IllegalArgumentException("For " + document); 
    } 

    public static Collection<ORID> getReferenceCollection(ODocument document, String className, String fieldName) { 

    if (isValid(document, className)) { 
     try { 
     Collection<OIdentifiable> result = document.field(fieldName, OType.LINKSET); 
     if (result == null) { 
      return Collections.emptySet(); 
     } 
     return result.stream().map(identifiable -> identifiable.getIdentity()) 
      .collect(Collectors.toCollection(ArrayList::new)); 
     } catch (Exception e) { 
     logger.error(e.getLocalizedMessage(), e); 
     return Collections.emptySet(); 
     } 
    } 
    throw new IllegalArgumentException("For " + document); 
    } 

    public static boolean isValid(ODocument document, String className) { 

    return Documents.isValid(document, className); 
    } 

    /** 
    * @return {@link Collection} of {@link ORID ORIDs} of all documents that 
    *   have been deleted during this operation 
    */ 
    public static Collection<ORID> releaseMany2ManyRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, Collection<? extends ODocument> documentsSecond, String classNameSecond, 
     String fieldNameSecond, boolean autoDeleteEmpty) { 

    Collection<ORID> result = new ArrayList<>(); 
    for (ODocument documentSecond : documentsSecond) { 
     // left side 
     boolean documentFirstIsEmpty = removeFromReferenceCollection(documentFirst, classNameFirst, fieldNameFirst, 
      documentSecond.getIdentity()); 
     if (documentFirstIsEmpty && autoDeleteEmpty && !result.contains(documentFirst.getIdentity())) { 
     result.add(documentFirst.delete().save().getIdentity()); 
     } 
     // right side 
     boolean documentSecondIsEmpty = removeFromReferenceCollection(documentSecond, classNameSecond, 
      fieldNameSecond, documentFirst.getIdentity()); 
     if (documentSecondIsEmpty && autoDeleteEmpty && !result.contains(documentSecond.getIdentity())) { 
     result.add(documentSecond.delete().save().getIdentity()); 
     } 
    } 
    if (!result.isEmpty()) { 
     logger.debug("Auto-deleted empty: " + result); 
    } 
    return result; 
    } 

    /** 
    * @return {@link Collection} of {@link ORID ORIDs} of all documents that 
    *   have been deleted during this operation 
    */ 
    public static Collection<ORID> releaseMany2ManyRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond, 
     boolean autoDeleteEmpty) { 

    return releaseMany2ManyRelationship(documentFirst, classNameFirst, fieldNameFirst, 
     Arrays.asList(documentSecond), classNameSecond, fieldNameSecond, autoDeleteEmpty); 
    } 

    public static void releaseMany2OneRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    // left side 
    setReference(documentFirst, classNameFirst, fieldNameFirst, null); 
    // right side 
    removeFromReferenceCollection(documentSecond, classNameSecond, fieldNameSecond, documentFirst.getIdentity()); 
    } 

    public static void releaseOne2ManyRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, Collection<? extends ODocument> documentsSecond, String classNameSecond, 
     String fieldNameSecond) { 

    for (ODocument documentSecond : documentsSecond) { 
     // left side 
     removeFromReferenceCollection(documentFirst, classNameFirst, fieldNameFirst, documentSecond.getIdentity()); 
     // right side 
     setReference(documentSecond, classNameSecond, fieldNameSecond, null); 
    } 
    } 

    public static void releaseOne2ManyRelationship(ODocument documentFirst, String classNameFirst, 
     String fieldNameFirst, ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    releaseOne2ManyRelationship(documentFirst, classNameFirst, fieldNameFirst, Arrays.asList(documentSecond), 
     classNameSecond, fieldNameSecond); 
    } 

    public static void releaseOne2OneRelationship(ODocument documentFirst, String classNameFirst, String fieldNameFirst, 
     Collection<? extends ODocument> documentsSecond, String classNameSecond, String fieldNameSecond) { 

    // left side 
    setReference(documentFirst, classNameFirst, fieldNameFirst, null); 
    // right side 
    for (ODocument documentSecond : documentsSecond) { 
     setReference(documentSecond, classNameSecond, fieldNameSecond, null); 
    } 
    } 

    public static void releaseOne2OneRelationship(ODocument documentFirst, String classNameFirst, String fieldNameFirst, 
     ODocument documentSecond, String classNameSecond, String fieldNameSecond) { 

    releaseOne2OneRelationship(documentFirst, classNameFirst, fieldNameFirst, Arrays.asList(documentSecond), 
     classNameSecond, fieldNameSecond); 
    } 

    /** 
    * @return {@code true} if reference collection is now empty; {@code false} 
    *   otherwise 
    */ 
    private static boolean removeFromReferenceCollection(ODocument orid, String className, String fieldName, 
     ORID value) { 

    Collection<ORID> refCollection = new HashSet<>(getReferenceCollection(orid, className, fieldName)); 
    refCollection.remove(value); 
    setReferenceCollection(orid, className, fieldName, refCollection); 
    return refCollection.isEmpty(); 
    } 

    public static void setReference(ODocument document, String className, String fieldName, OIdentifiable reference) { 

    if (isValid(document, className)) { 
     document.field(fieldName, reference, OType.LINK).save(); 
    } else 
     throw new IllegalArgumentException("For " + document); 
    } 

    private static void setReferenceCollection(ODocument document, String className, String fieldName, 
     Collection<? extends ORID> value) { 

    if (isValid(document, className)) { 
     document.field(fieldName, value, OType.LINKSET).save(); 
    } else 
     throw new IllegalArgumentException("For " + document); 
    } 
}