【问题标题】:Mapping hierarchial objects in MongoDB using Morphia使用 Morphia 在 MongoDB 中映射分层对象
【发布时间】:2014-05-22 11:54:23
【问题描述】:

假设我在 MongoDB 中有一个存储员工列表的实体。

@Entitiy
public class EmployeeList{
    @Embedded
    List<Employee> employeeList;
}

Employee 是一个具有一些属性的抽象类。

public abstract class Employee{
    String name;
    String emailId;
}

有不同类型的员工 - 开发人员、设计师、人力资源

class Developer extends Employee{
    String githubProfile;
}


class Designer extends Employee{
   String portfolio;
}


class HumanResource extends Employee{
   String department; 
}

如果 mongo 包含开发人员、设计人员和人力资源人员的列表,Morphia 可以将它们映射到相应的类吗? 例如,如果 db 有以下数据 -

[{'name':'p1', 'emailId':'p1@x1", 'portfolio':'http://abc.co'},
{'name':'p2', 'emailId':'p2@x1", 'department':'finance'},
{'name':'p3', 'emailId':'p3@x1", 'githubProfile':'http://github.com/p3'}]

当 Morphia 将此集合映射到 EmployeeList 实体时,我如何确保它们映射到相应的类?

【问题讨论】:

    标签: java mongodb morphia odm


    【解决方案1】:

    当您将员工添加到列表中时,您可能会执行以下操作。

    employeeList.add(new Developer(...))
    employeeList.add(new Designer(...))
    employeeList.add(new HumanResource(...))
    

    然后将实体保存在 morphia 中,它应该可以工作。

    PS:这个我没试过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多