0

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

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

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

4

5 回答 5

0

只有当您从 RDBMS 导入并将 FOREIGN KEYS 转换为链接时,才会创建链接。要创建链接,只需将目标的 RID 放入源对象中。您使用的是什么 API?

于 2013-05-02T10:25:12.890 回答
0

我有一个简单的例子简单的例子

我有两张桌子。发布和评论。

表帖:

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

在表中帖子将更改为:

表帖:

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

谢谢 !

于 2013-05-03T01:28:06.197 回答
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;

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

于 2015-09-29T06:50:57.783 回答
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);
    }
}
于 2016-12-13T16:27:02.957 回答
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,所以我怀疑这是正确的方法。

于 2016-04-25T13:00:43.383 回答