...
1 //...此處省略了import... 2 /** 3 * Created by shenzhigang on 4/15/16. 4 */ 5 public class Main { 6 public static void main(String[] args) { 7 Datastore datastore; 8 final Morphia morphia = new Morphia(); 9 morphia.mapPackage("com.bdb.datatypes.common.po"); 10 datastore = morphia.createDatastore(new MongoClient("localhost", 27017), "test001"); 11 datastore.ensureIndexes(); 12 13 Student student = new Student(); 14 student.setName("沈志剛"); 15 student.setAge(30); 16 17 School school = new School(); 18 school.setName("蘇州小學"); 19 school.setAddress("蘇州市"); 20 datastore.save(school); 21 22 student.setSchool(school); 23 datastore.save(student); 24 25 List<Key<School>> schools =datastore.createQuery(School.class).field("name").equal("蘇州小學").asKeyList(); 26 List<Student> studentList = datastore.find(Student.class).field("school").in(schools).asList(); 27 } 28 } 29 30 @Entity 31 class Student { 32 @Id 33 private ObjectId objectId ; 34 private String name; 35 private int age; 36 @Reference 37 private School school; 38 //...此處省略了get set.... 39 } 40 41 @Entity 42 class School { 43 @Id 44 private ObjectId objectId; 45 private String address; 46 private String name; 47 //....此處省略了get set... 48 } 49