2014-04-09 5 views
1

직원 목록을 저장하는 MongoDB 엔티티가 있다고 가정 해 봅니다.Morphia를 사용하는 MongoDB의 계층 적 객체 매핑

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

직원은 일부 속성을 가진 추상 클래스입니다. 다른 직원의 유형이있다

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

- 개발자, 디자이너, HumanResource

class Developer extends Employee{ 
    String githubProfile; 
} 


class Designer extends Employee{ 
    String portfolio; 
} 


class HumanResource extends Employee{ 
    String department; 
} 

는 몽고 개발자, 디자이너, humanResource 사람들의 목록이 포함되어있는 경우, 모르핀은 해당 클래스에 매핑 할 수 있습니까? DB를 다음 데이터가있는 경우 예를 들어, -이 컬렉션은 EmployeeList 엔티티에 모르핀에 의해 매핑되는

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

을, 어떻게 그들이 해당 클래스에 매핑되어 있는지 확인합니까?

답변

1

직원을 목록에 추가 할 때 다음과 같은 작업을 수행 할 수 있습니다.

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

엔피디아를 모르 피아에 저장하면 효과가 있습니다.

추신 : 나는 이것을 시도하지 않았습니다.

+1

예! 이 작품! 아이디어에 감사드립니다! :) – shiladitya