完成Morphia / MongoDB示例代码

时间:2013-08-24 10:09:44

标签: java mongodb morphia

我一直试图从Morphia网站获取示例代码但收效甚微,我想知道为什么下面的代码片段失败了?

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;
}

然而,当使用上面的代码时,“获得没有经理的员工”行无法找到任何内容,更新操作会引发异常。任何帮助将不胜感激?

1 个答案:

答案 0 :(得分:2)

  1. 我认为应该是Employee boss = ds.find(Employee.class).field("manager").doesNotExist().get();

  2. 如果我没弄错,实体应该有一个no-args构造函数。

  3. PS:如果您想快速开始完整项目,可能需要查看https://github.com/xeraa/mongouk2011