2014-01-15 30 views
1

我有一個嵌入式文檔的OrientDB數據庫與記錄,我不能回讀保存的實體。獲取正確數量的元素,但屬性未映射到我的pojo字段。 我在做什麼錯?有小費嗎?Java對象與記錄之間的綁定

謝謝!

 OObjectDatabaseTx db = new OObjectDatabaseTx("local:db"); 
     if (db.exists()) { 
      db = new OObjectDatabaseTx("local:db").open("admin", "admin"); 
     } else { 
      db.create(); 
     } 

     db.getEntityManager().registerEntityClass(User.class); 

     long cnt = db.countClass(User.class); 
     System.out.println(cnt); // OK 

     User user = db.newInstance(User.class, "Firstname", "Lastname", "[email protected]"); 

     db.begin(); 
     db.save(user); // it's OK, the DB contains this document, I can select it with the servers console 
     db.commit(); 

     for (User usr : db.browseClass(User.class)) { 
      System.out.println(usr); // User object with null fields, why? 
     } 

用戶等級:

public class User { 

private String firstname; 

private String lastname; 

private String email; 

public User() { 
} 

public User(String firstname, String lastname, String email) { 
    this.firstname = firstname; 
    this.lastname = lastname; 
    this.email = email; 
} 

public String getFirstname() { 
    return firstname; 
} 

public void setFirstname(String firstname) { 
    this.firstname = firstname; 
} 

public String getLastname() { 
    return lastname; 
} 

public void setLastname(String lastname) { 
    this.lastname = lastname; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

@Override 
public String toString() { 
    return firstname + " " + lastname + " " + email; 
} 

}

回答

1

Java對象直接從OrientDB檢索(如你在這個例子做)的代理透明自己的基礎記錄,使延遲加載他們的領域。在您的方案中,您必須使用User對象get方法才能從數據庫中觸發相應字段的填充。這些字段有默認值(全部爲null)直到加載。

參見:OrientDB Object2RecordJavaBinding

+0

感謝您的答案! – Rstrl