问题
I am trying to map a hashset from PO object to list in model object using dozer mapping.
This is my source and destination class :-
public class Source implements Serializable {
Set<NoteVariableDefinitionPOImpl> noteVariableDefinitionPO = new HashSet<NoteVariableDefinitionPOImpl>();
}
public class Destination implements Serializable {
List<NoteVariableDefinition> noteVariableDefinition = new ArrayList<NoteVariableDefinition)();
NoteVariableDefinitionPOImpl class :
class NoteVariableDefinitionPOImpl {
int x; // setter + getter
}
NoteVariableDefinition class :
class NoteVariableDefinition {
int y ;
// setter + getter
}
My dozer mapping configuration is :
<mapping wildcard="false" map-id="NC.Source.Destination" map-null="false">
<class-a>test.Source</class-a>
<class-b>test.Destination</class-b>
<field map-id="NC.NoteVariableDefinitionPOImpl.NoteVariableDefinition">
<a>noteVariableDefinitionPO</a>
<b>noteVariableDefinition</b>
<a-hint>java.util.HashSet</a-hint>
<b-hint>java.util.ArrayList</b-hint>
</field>
</mapping>
<mapping wildcard="false"
map-id="NC.NoteVariableDefinitionPO.NoteVariableDefinition" type="one-way" map-null="false">
<class-a>com.test.po.impl.NoteVariableDefinitionPOImpl</class-a>
<class-b>com.test.model.NoteVariableDefinition</class-b>
<field>
<a>x</a>
<b>y</b>
</field>
</mapping>
Whenever I run this mapping , I always get the following error :
MapId: NC.NoteVariableDefinitionPO.NoteVariableDefinition
Type: null
Source parent class: test.Source
Source field name: noteVariableDefinitionPO
Source field type: class java.util.HashSet
Source field value: [com.test.po.impl.NoteVariableDefinitionPOImpl@e21649a8]
Dest parent class: test.Destination
Dest field name: noteVariableDefinition
Dest field type: java.util.List
org.dozer.MappingException: Class mapping not found by map-id: SRC-CLASS- com.test.po.impl.NoteVariableDefinitionPOImpl DST-CLASS-java.util.ArrayList MAP-ID->NC.NoteVariableDefinitionPO.NoteVariableDefinition
How do I map a HashSet to ArrayList in this case?
I am new to dozer mapping and hence unable to define a custom converter as well.
Dozer documentation says that it can convert a Set to List
Dozer automatically maps between collection types and automatically performs any type conversion.
The following collection mapping is automatically handled by Dozer: (These are >>all bi-directional) :
Set to List
回答1:
Try this :-
First define a custom converter :
package com.test.converter;
public class Converter extends DozerConverter<Set, List> {
@Autowired
Mapper mapper;
public Converter() {
super(Set.class, List.class);
}
@Override
public List convertTo(Set noteVariableDefinitionPO, List noteVariableDefinition) {
//iterate set
NoteVariableDefinition mappedValue = mapper
.map(noteVarDefinitionPO,
NoteVariableDefinition.class,
mappingID);
// add it to list and return
}
@Override
public Set convertFrom(List arg0, Set arg1) {
return null;
}
}
Then define the following global configuration :
<mappings>
<configuration>
<custom-converters>
<converter type= "com.test.converter.Converter" (path to custom converter)>
<class-a>java.util.Set </class-a>
<class-b>java.util.List </class-b>
</converter>
</custom-converters>
</configuration>
</mappings>
And change your field value as :
<field map-id="NC.NoteVariableDefinitionPO.NoteVariableDefinition"
custom-converter= "com.test.converter.Converter" (path to custom converter)>
<a>noteVariableDefinitionPO</a>
<b>noteVariableDefinition</b>
<a-hint>java.util.HashSet</a-hint>
<b-hint>java.util.ArrayList</b-hint>
</field>
来源:https://stackoverflow.com/questions/39676018/dozer-mapping-from-hashset-to-arraylist