如何使用 morphia 加载参考文档/实体的两个属性
我有一个班级人物和学校,就像这个
人物班级
@Entity
public class Person {
@Id private ObjectId id;
private String name;
@Embedded private PersonEducation schoolInfo;
}
@Embedded
public class PersonEduction {
@Reference private School school;
private String year;
private String degree;
}
学校班
@Entity
public class School {
@Id private ObjectId id;
private String name;
private String address;
private String description;
}
我如何在 Person 类示例中获取 School 的 id 和 name 字段当我想找人时
Person person = datastore.find(Person.class).field("name").equals("xyz").get();
person.gerSchoolInfo();
响应(不想要所有 School 类字段)
{school:{_id:ObjectId("4fcef3e20364a375e7631fb0"), name:"SchoolA"}, year:"1990", degree:"MBA" }
如果我查询 _id=ObjectId("4fcef3e20364a375e7631fb0") 的学校,我会得到所有学校字段
{_id:ObjectId("4fcef3e20364a375e7631fb0"), name:"xyz", address="some add", description="some desc"}