我正在尝试使用dozer映射将PO对象中的hashset映射到模型对象中的列表。
这是我的来源和目的地类: -
public class Source implements Serializable {
Set<NoteVariableDefinitionPOImpl> noteVariableDefinitionPO = new HashSet<NoteVariableDefinitionPOImpl>();
}
public class Destination implements Serializable {
List<NoteVariableDefinition> noteVariableDefinition = new ArrayList<NoteVariableDefinition)();
NoteVariableDefinitionPOImpl类:
class NoteVariableDefinitionPOImpl {
int x; // setter + getter
}
NoteVariableDefinition类:
class NoteVariableDefinition {
int y ;
// setter + getter
}
我的推土机映射配置是:
<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>
每当我运行此映射时,我总是会收到以下错误:
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
在这种情况下,如何将HashSet映射到ArrayList?
我是推土机映射的新手,因此无法定义自定义转换器。
Dozer文档说它可以将Set转换为List
Dozer会自动在集合类型之间进行映射,并自动执行任何类型转换。
以下集合映射由Dozer自动处理:(这些是&gt;&gt;所有双向):
设为列表
答案 0 :(得分:3)
试试这个: -
首先定义自定义转换器:
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;
}
}
然后定义以下全局配置:
<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>
并将您的字段值更改为:
<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>