gpt4 book ai didi

java - 无法将文档作为 Java 类读取

转载 作者:行者123 更新时间:2023-11-30 06:34:04 24 4
gpt4 key购买 nike

我正在使用 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

最佳答案

我可以通过将 MyObject 移动到它自己的文件来使您的示例正常工作。我认为这可能是由于内联对象造成的,因为我尝试内联添加注释和 getters/setters,但这也不起作用。就像这样:

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

关于java - 无法将文档作为 Java 类读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com