Morphia - 将单个集合数据映射到多个 pojo

Morphia - Map single collection data into multiple pojos

我有一个包含 40 个字段的集合。

我有如下pojo

@Entity("colName")
public class Entity1 {
   //Id
   //10 fields - names same as collection columns
   @Embedded
   //Entity2
}

实体 2 class:

@Embedded
public class Entity2 {
  //20 other fields
}

我查询集合如下

datastore.createQuery(Entity1.class).disableValidation().filter("fieldFromEntity1", "2227536").asList();

但我总是得到 Entity2 引用为空。

我发现Entity2必须是嵌套文档。

有什么办法可以实现吗?

文件:

{
 _id: a|b|c,
 field1: stringValue,
 ....
 field40: intgerValue
}

Morphia 不直接支持您的需求。但是,您可以使用 @Preload@Presave 在 to/from 数据库中操作文档的形状。我认为您也可以在序列化为 json 时,对 name/nest 这些字段使用 Jackson 注释。那将是最简单的方法。