我在東方數據庫中遇到了問題。如何在Orient-DB中創建關係?
我的問題是我不知道如何在orient-DB中創建關係?
我讀過文檔,看到它有命令CREATE LINK。但我不知道它如何創建鏈接,當我插入新的記錄,並不運行命令CREATE LINK。
我在東方數據庫中遇到了問題。如何在Orient-DB中創建關係?
我的問題是我不知道如何在orient-DB中創建關係?
我讀過文檔,看到它有命令CREATE LINK。但我不知道它如何創建鏈接,當我插入新的記錄,並不運行命令CREATE LINK。
CREATE LINK僅當您從RDBMS導入並將FOREIGN KEYS轉換爲鏈接時。要創建鏈接,只需將目標的RID放入源對象。你在使用什麼API?
我舉個簡單的例子簡單
我有兩個表。發佈和評論。
表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]
+----+----------------++----------------+
謝謝!
爲什麼你發佈你的問題作爲答案 – 2015-09-29 06:40:54
你應該使用這個
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 **********/
我正在使用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或相當頻繁,所以我懷疑這是做正確的方式。
是的,我也想知道這一點。據我所知,文檔並沒有解釋,對於文檔api,如何實際添加關係。 – 2016-12-12 15:16:14
現在我正在使用這個類,它按預期工作到目前爲止。
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);
}
}
嗨Lvca! 我做的例子關注這個頁面https://code.google.com/p/orient/wiki/ImportFromRDBMS 而當我想插入一條記錄來評論表中的新紀錄。 我的問題是我不知道如何對帖子表的字段評論可以更新? 謝謝! – Hyeongsik 2013-05-02 10:52:56
對不起,我還沒有明白,你能說得更好嗎? – Lvca 2013-05-02 11:23:05