//...此处省略了import...
/**
* Created by shenzhigang on 4/15/16.
*/
public class Main {
public static void main(String[] args) {
Datastore datastore;
final Morphia morphia = new Morphia();
morphia.mapPackage("com.bdb.datatypes.common.po");
datastore = morphia.createDatastore(new MongoClient("localhost", 27017), "test001");
datastore.ensureIndexes();
Student student = new Student();
student.setName("沈志刚");
student.setAge(30);
School school = new School();
school.setName("苏州小学");
school.setAddress("苏州市");
datastore.save(school);
student.setSchool(school);
datastore.save(student);
List<Key<School>> schools =datastore.createQuery(School.class).field("name").equal("苏州小学").asKeyList();
List<Student> studentList = datastore.find(Student.class).field("school").in(schools).asList();
}
}
@Entity
class Student {
@Id
private ObjectId objectId ;
private String name;
private int age;
@Reference
private School school;
//...此处省略了get set....
}
@Entity
class School {
@Id
private ObjectId objectId;
private String address;
private String name;
//....此处省略了get set...
}