Java 完成Morphia/MongoDB示例代码

Java 完成Morphia/MongoDB示例代码,java,mongodb,morphia,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 =

我一直在尝试从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();
            }
        }
    }
公共类DBUtil{
私有静态Mongo-Mongo;
私有静态数据存储;
私人静态睡眠;
公共静态void main(字符串[]args){
试一试{
mongo=新的MongoClient(新服务器地址(Consts.DatabaseHost,
常数数据库端口);
morphia=新morphia();
morphia.map(Employee.class);
ds=morphia.createDatastore(mongo,Consts.DatabaseName);
DB DB=Mongo.connect(新DBAddress(Consts.DatabaseHost,
Consts.DatabasePort,Consts.DatabaseName));
ds.save(新员工(“先生”,“上帝”,空,0));
//找一个没有经理的员工
Employee boss=ds.find(Employee.class).field(“经理”)
.equal(null).get();
Key scottsKey=ds.save(新员工(“Scott”,
“Hernandez”,ds.getKey(boss),150*1000);
//将Scott添加为其经理的员工
UpdateResults res=ds.update(
老板,
ds.createUpdateOperations(Employee.class).add(“下属”,
斯科茨基);
//找Scott的老板;和上面的一样。
Employee scottsBoss=ds.find(Employee.class)
.filter(“下划线”,scottsKey).get();
for(Employee e:ds.find(Employee.class,“manager”,boss))
系统输出打印ln(e);
}捕获(未知后异常e1){
e1.printStackTrace();
}
}
}
当对employee类使用以下内容时

@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;
}
@实体(“员工”)
班级员工{
公共雇员(字符串firstName、字符串lastName、对象Object、int i){
this.firstName=firstName;
this.lastName=lastName;
manager=新密钥(Employee.class,object);
}
//自动生成,如果未设置(请参见ObjectId)
@Id ObjectId Id;
//值类型将自动持久化
字符串firstName,lastName;
//仅存储非空值
长期工资=空;
//默认情况下,字段为@Embedded
地址;
//无需自动加载即可保存引用
关键经理;
//引用被存储**,并自动加载
@参考列表下划线=新的ArrayList();
//存储在一个二进制字段中
//@序列化的EncryptedReviews;
//可以重命名字段
@财产(“开始”)日期开始日期;
@财产(“左”)日期结束日期;
//可以为字段编制索引以获得更好的性能
@索引布尔活动=假;
//可以加载字段,但不能保存
@NotSaved字符串读取但NotStored;
//可以忽略字段(无加载/保存)
@不存储瞬态int;
//例如,not@Transient将被序列化/GWT忽略。
瞬时布尔存储=真;
}
但是,当使用上述代码时,“获取没有经理的员工”行无法找到任何内容,更新操作引发异常。如果有任何帮助,我们将不胜感激

  • 我认为应该是
    Employee boss=ds.find(Employee.class).field(“manager”).doesNotExist().get()

  • 如果我没有弄错的话,实体应该有一个无参数构造函数


  • PS:如果您想快速开始一个完整的项目,您可能想看看

    ,因此我下载了您的项目,并注意到它成功地将实体保存到数据库中。此后,我使用了您的代码来回溯,并意识到我在项目中使用GWT一直在妨碍我,因为我一直在使用@Id字符串,这打破了morphia。morphia for GWT的v1.02根本不起作用。