问题
I am using Dozer to map some beans, and I have a mapping that I can't figure out.
Here are my classes:
class A{
private ComplexeType type;
//Constructors & getters
}
class B{
private String[] type;
//Constructors & getters
}
class ComplexteType{
private List<String> list;
//Getter for the list, no constructor
}
How do I map class A to class B?
I want to map the field type of class A to the field type of the class B using xml.
here is the xml file:
<mapping>
<class-a>A</class-a>
<class-b>B</class-b>
<field custom-converter="AToBCustomConverter">
<a>type</a>
<b>type</b>
</field>
</mapping>
And here is a sippet from my CustomConverter
if (source == null) {
return null;
}
B dest = null;
if (source instanceof java.lang.String) {
// check to see if the object already exists
if (destination == null) {
dest = new A();
} else {
dest = (A) destination;
}
dest.getTypes().add((String) source);
return dest;
} else if (source instanceof B) {
String[] sourceObj = ((B) destination)
.getType()
.toArray(
new String[((B) destination)
.getType().size()]);
return sourceObj;
} else {
throw new MappingException(
"Converter StatResultCustomConverter used incorrectly. Arguments passed in were:"
+ destination + " and " + source);
}
}
回答1:
I don't think your CustomConverter
is necessary in this case, see here.
Try this in your mapping file:
<mapping>
<class-a>A</class-a>
<class-b>B</class-b>
<field>
<a>type.list</a>
<b>type</b>
</field>
</mapping>
and Dozer should perform the nested mapping automatically.
回答2:
Here is the mapping I used to solve the problem.
<mapping>
<class-a>Q</class-a>
<class-b>B</class-b>
<field>
<a is-accessible="true">type<list</a>
<b is-accessible="true">type</b>
</field>
</mapping>
来源:https://stackoverflow.com/questions/10016304/how-to-mapp-an-array-to-a-complex-type-using-dozer