2017-08-04 92 views
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 

回答

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

谢谢!这个解决方案适用于我。将这个类移动到一个单独的文件中,解决了文档未被加载的问题,同时使用“DocumentField”(我在某些文档中错过了这个部分[https://github.com/arangodb/arangodb-java-驱动程序#序列化))允许解决返回的“null”键的其他问题。 – Guiux