2013-08-24 67 views
3

我一直在嘗試從Morphia網站獲得示例代碼,但很少成功,我想知道爲什麼下面的代碼片段失敗?完成Morphia/MongoDB示例代碼

public class DBUtil { 
    private static Mongo mongo; 
    private static Datastore ds; 
    private static Morphia morphia; 

    public static void main(String[] args) { 
     try { 
      mongo = new MongoClient(new ServerAddress(Consts.DatabaseHost, 
        Consts.DatabasePort)); 

      morphia = new Morphia(); 

      morphia.map(Employee.class); 

      ds = morphia.createDatastore(mongo, Consts.DatabaseName); 

      DB db = Mongo.connect(new DBAddress(Consts.DatabaseHost, 
        Consts.DatabasePort, Consts.DatabaseName)); 

      ds.save(new Employee("Mister", "GOD", null, 0)); 

      // get an employee without a manager 
      Employee boss = ds.find(Employee.class).field("manager") 
        .equal(null).get(); 

      Key<Employee> scottsKey = ds.save(new Employee("Scott", 
        "Hernandez", ds.getKey(boss), 150 * 1000)); 

      // add Scott as an employee of his manager 
      UpdateResults<Employee> res = ds.update(
        boss, 
        ds.createUpdateOperations(Employee.class).add("underlings", 
          scottsKey)); 

      // get Scott's boss; the same as the one above. 
      Employee scottsBoss = ds.find(Employee.class) 
        .filter("underlings", scottsKey).get(); 

      for (Employee e : ds.find(Employee.class, "manager", boss)) 
        System.out.println(e); 

      } catch (UnknownHostException e1) { 
       e1.printStackTrace(); 
      } 
     } 
    } 

當僱員類使用以下內容?

@Entity("employees") 
class Employee { 
    public Employee(String firstName, String lastName, Object object, int i) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     manager = new Key<Employee>(Employee.class, object); 
    } 

    // auto-generated, if not set (see ObjectId) 
    @Id ObjectId id; 

    // value types are automatically persisted 
    String firstName, lastName; 

    // only non-null values are stored 
    Long salary = null; 

    // by default fields are @Embedded 
    Address address; 

    //references can be saved without automatic loading 
    Key<Employee> manager; 

    //refs are stored**, and loaded automatically 
    @Reference List<Employee> underlings = new ArrayList<Employee>(); 

    // stored in one binary field 
    //@Serialized EncryptedReviews; 

    //fields can be renamed 
    @Property("started") Date startDate; 
    @Property("left") Date endDate; 

    //fields can be indexed for better performance 
    @Indexed boolean active = false; 

    //fields can loaded, but not saved 
    @NotSaved String readButNotStored; 

    //fields can be ignored (no load/save) 
    @Transient int notStored; 

    //not @Transient, will be ignored by Serialization/GWT for example. 
    transient boolean stored = true; 
} 

使用上面的代碼然而,當,在「讓員工沒有經理人行未能找到任何與更新操作拋出異常。任何幫助,將不勝感激?

回答

2
  1. 我覺得應該是Employee boss = ds.find(Employee.class).field("manager").doesNotExist().get();

  2. 如果我沒有記錯的話,實體應該有一個無參數的構造函數。

PS:如果你想盡快開始使用一個完整的項目,你可能想看看https://github.com/xeraa/mongouk2011

+0

於是我下載了你的項目,並注意到它成功保存實體到數據庫。我已經使用你的代碼來回溯,並意識到我在項目中使用GWT已經成爲阻礙因爲我已經使用了@Id的字符串,並打破了morphia。 GWT的morphia v1.02根本不起作用。 – hegsie