gpt4 book ai didi

java - 从 HashSet 到 Arraylist 的 Dozer 映射

转载 作者:行者123 更新时间:2023-12-01 21:17:50 27 4
gpt4 key购买 nike

我正在尝试使用推土机映射将哈希集从 PO 对象映射到模型对象中的列表。

这是我的源类和目标类:-

 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 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

最佳答案

试试这个:-

首先定义一个自定义转换器:

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>

关于java - 从 HashSet 到 Arraylist 的 Dozer 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39676018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com