我正在使用ArangoDB 3.1.23和ArangoDB Java驱动程序4.2.2。我正在使用Eclipse和Maven。我在阅读文档作为Java类时遇到了麻烦,正如here所解释的那样。我按照教程编写了以下测试代码。
正如您所看到的,将文档读取为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
答案 0 :(得分:1)
我可以通过将MyObject
移动到自己的文件来让您的示例正常工作。我认为这可能是由于内联对象,因为我尝试添加注释和getters / setter内联,但也没有。像这样:
import com.arangodb.entity.DocumentField;
import com.arangodb.entity.DocumentField.Type;
public class MyObject {
@DocumentField(Type.KEY)
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
public MyObject(String name, int age) {
this();
this.name = name;
this.age = age;
}
public MyObject() {
super();
}
}
和
// 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.getName() + " " + doc3.getAge());
else
System.err.println("Could not open the document " + keyToRead3 + " as MyObject");
哪个产生
Inserted new document as MyObject. key: 7498620, 7498620
Inserted new document as BaseDocument. key: 7498624, 7498624
Open document 7498620 VPackSlice: Homer 38
Open document 7498620 as BaseDocument: Homer 38
Open document 7498620 as MyObject: Homer 38