1
我正在使用ArangoDB 3.1.23和ArangoDB Java驅動程序4.2.2。我使用Eclipse和Maven。我有麻煩閱讀文檔作爲Java類,因爲它是解釋here。我按照教程編寫了以下測試代碼。無法將文檔讀取爲Java類
正如您所看到的,將文檔作爲BaseDocument或VelocyPack工作,但將它們作爲Java類讀取會返回null。
public static void main(String[] args) {
class MyObject {
private String key;
private String name;
private int age;
public MyObject(String name, int age) {
this();
this.name = name;
this.age = age;
}
public MyObject() {
super();
}
}
final String dbName = "testdb";
final String collName = "testCollection";
ArangoDB arangoDB = new ArangoDB.Builder().user("root").password("").build();
// Delete existing database
try{
System.out.println("Deleted existing " + dbName + " database: " + arangoDB.db(dbName).drop());
} catch (Exception e) {
System.err.println("Error while deleting database " + dbName);
}
// Test database creation
try {
arangoDB.createDatabase(dbName);
System.out.println("Created database " + dbName);
} catch (Exception e) {
System.err.println("Did not create database " + dbName);
}
// Test collection creation
try {
arangoDB.db(dbName).createCollection(collName);
System.out.println("Created collection " + collName);
} catch (Exception e) {
System.err.println("Did not create collection " + collName);
}
// Test custom class document insertion
String key1 = null;
try {
MyObject myObject = new MyObject("Homer", 38);
key1 = arangoDB.db(dbName).collection(collName).insertDocument(myObject).getKey();
System.out.println("Inserted new document as MyObject. key: " + myObject.key + ", " + key1);
} catch (Exception e) {
System.err.println("Did not insert new document");
}
// Test BaseDocument class document insertion
String key2 = null;
try {
BaseDocument myBaseDocument = new BaseDocument();
myBaseDocument.addAttribute("name", "Paul");
myBaseDocument.addAttribute("age", 23);
key2 = arangoDB.db(dbName).collection(collName).insertDocument(myBaseDocument).getKey();
System.out.println("Inserted new document as BaseDocument. key: " + myBaseDocument.getKey() + ", " + key2);
} catch (Exception e) {
System.err.println("Did not insert new document");
}
// Test read as VPackSlice
String keyToRead1 = key1;
VPackSlice doc1 = arangoDB.db(dbName).collection(collName).getDocument(keyToRead1, VPackSlice.class);
if (doc1 != null)
System.out.println("Open document " + keyToRead1 + " VPackSlice: " + doc1.get("name").getAsString() + " " + doc1.get("age").getAsInt());
else
System.err.println("Could not open the document " + keyToRead1 + " using VPackSlice");
// Test read as BaseDocument
String keyToRead2 = key1;
BaseDocument doc2 = arangoDB.db(dbName).collection(collName).getDocument(keyToRead2, BaseDocument.class);
if (doc2 != null)
System.out.println("Open document " + keyToRead2 + " as BaseDocument: " + doc2.getAttribute("name") + " " + doc2.getAttribute("age"));
else
System.err.println("Could not open the document " + keyToRead2 + " as BaseDocument");
// Test read as MyObject
String keyToRead3 = key1;
MyObject doc3 = arangoDB.db(dbName).collection(collName).getDocument(keyToRead3, MyObject.class);
if (doc3 != null)
System.out.println("Open document " + keyToRead3 + " as MyObject: " + doc3.name + " " + doc3.age);
else
System.err.println("Could not open the document " + keyToRead3 + " as MyObject");
}
結果:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Deleted existing testdb database: true
Created database testdb
Created collection testCollection
Inserted new document as MyObject. key: null, 3510088
Inserted new document as BaseDocument. key: 3510092, 3510092
Open document 3510088 VPackSlice: Homer 38
Open document 3510088 as BaseDocument: Homer 38
Could not open the document 3510088 as MyObject
謝謝!這個解決方案適用於我。將這個類移動到一個單獨的文件中,解決了文檔未被加載的問題,同時使用「DocumentField」(我在某些文檔中錯過了這個部分[https://github.com/arangodb/arangodb-java-驅動程序#序列化))允許解決返回的「null」鍵的其他問題。 – Guiux