2013-08-06 3 views
0

도메인 객체 (DO)와 키 - 값 (KV) 객체가 있습니다. DO의 각 필드를 KV 인스턴스에 매핑하는 방법은 무엇입니까?매퍼 : 객체 대 키 - 값

예 :

class DomainObject { 
    String field1 = "value1"; 
    String field2 = "value2"; 
} 

class KeyValue { 
    String key; 
    String value; 
} 

입력 DomainObject이고, 출력 - 키 값의 두 인스턴스 (= 키 "필드 1"값 = "VALUE1"키이 = "FIELD2"값 = "값 2").
P. field-to-field 매핑을 위해 Dozer을 사용했지만 어떻게 필드 대 KV를 할 수 있습니까?

답변

0

아마도 DomainObject에서 KeyValue에 대한 참조를 만들 수 있습니다. 따라서 각 KeyValue는 DomainObject의 fields 목록에있는 항목에 매핑됩니다. 이것에 simmilar 뭔가 :

List<KeyValue> list = new ArrayList<KeyValue>(); 
list.add(new KeyValue("field1", "value1")); 
list.add(new KeyValue("field2", "value2"));  

DomainObject domainObject = new DomainObject(list);  

class DomainObject { 
    List<KeyValue> fields; 

    public DomainObject(List<KeyValue> keyValueList){ 
     this.fields=keyValueList; 
    } 

    public List<KeyValue> getFields() { 
     return fields; 
    } 
} 

class KeyValue { 
    String key; 
    String value; 

    public KeyValue(String key, String value) { 
     this.key=key; 
     this.value=value; 
    } 
} 

아니면이 작업의 이런 종류의 해시 맵을 사용할 수 http://tutorialswithexamples.com/java-map-and-hashmap-tutorial-with-examples/