首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >完成Morphia/MongoDB示例代码

完成Morphia/MongoDB示例代码
EN

Stack Overflow用户
提问于 2013-08-24 18:09:44
回答 1查看 9.2K关注 0票数 3

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

代码语言:javascript
复制
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();
            }
        }
    }

当为employee类使用以下内容时?

代码语言:javascript
复制
@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;
}

然而,当使用上面的代码时,'get an employee without a manager‘行找不到任何东西,更新操作抛出异常。如有任何帮助,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-24 20:58:31

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

  • If我没有弄错,实体应该有一个-
  1. 构造函数。

PS:如果你想快速开始一个完整的项目,你可能想看看https://github.com/xeraa/mongouk2011

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18417383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档