简体   繁体   中英

Dozer - using custom create method

I am not sure if I understand custom create method in Dozer mapper correctly. I need to translate bean property of type int to TransTypeCodebook object instance. But I am getting:

2013-09-13 15:47:27,009 [main] ERROR org.dozer.MappingProcessor  - Field mapping error    -->
MapId: null
Type: null
Source parent class: cz.jaksky.dozer.a.HolderA
Source field name: transType
Source field type: class java.lang.Integer
Source field value: 0
Dest parent class: cz.jaksky.dozer.b.HolderB
Dest field name: transTypeCodebook
Dest field type: cz.jaksky.dozer.b.codebook.TransTypeCodebook
org.dozer.MappingException: Illegal object type for the method 'setTransTypeCodebook'. 
Expected types: 
cz.jaksky.dozer.b.codebook.TransTypeCodebook
Actual types: 
java.lang.String

My TransTypeCodebook class

public class TransTypeCodebook extends Codebook {
private int code;
private String label;

private TransTypeCodebook(int code, String label) {
    this.code = code;
    this.label = label;
}

public int getCode() {
    return code;
}

public String getLabel() {
    return label;
}

public static TransTypeCodebook getCodebook(int code) {
    TransTypeCodebook result;

    switch (code) {
        case 0:
            result = new TransTypeCodebook(0, "Case0");
            break;
        case 1:
            result = new TransTypeCodebook(1, "Case1");
            break;
        default:
            result = new TransTypeCodebook(code, "Not a valid code");
    }

    return result;
}

}

Mapper portion

 <field>
        <a>transType</a>
        <b create-method="getCodebook">transTypeCodebook</b>
 </field>

I manged to solve this issue by custom converters but I am not sure if I understand the concept of custom create method and more over I am wondering from where that String is comming. Can anyone put light on that?

I am not exactly sure, but to use static methods like that, you need to specify its fully qualified name.

<b create-method="your.domain.TransTypeCodebook.getCodebook">transTypeCodebook</b>

That's stated in the Dozer documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM