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

时间:2013-05-02 08:59:41

标签: graph relationships graph-databases orientdb

我在Orient-DB中遇到了关系。

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

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

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我有一个简单的示例简单

我有两张桌子。发表评论。

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

所以现在。我运行命令:

 CREATE LINK comments TYPE linkset FROM comment.postId To post.id INVERSE.

在表格中将更改为:

TABLE POST:

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

所以现在。我想在评论表中插入一条记录。

我运行此命令:

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

谢谢!

答案 2 :(得分: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 ******* *** /

答案 3 :(得分:0)

我正在使用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();
    }
}

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

答案 4 :(得分: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);
    }
}