java - Dozer bidirectional mapping (String, String) with custom comverter impossible? -
i have dozer mapping custom converter:
<mapping> <class-a>com.xyz.customer</class-a> <class-b>com.xyz.customerdao</class-b> <field custom-converter="com.xyz.dozeremptystring2nullconverter"> <a>customername</a> <b>customername</b> </field> </mapping>
and converter:
public class dozeremptystring2nullconverter extends dozerconverter<string, string> { public dozeremptystring2nullconverter() { super(string.class, string.class); } public string convertfrom(string source, string destination) { string ret = null; if (source != null) { if (!source.equals("")) { ret = stringformatter.wildcard(source); } } return ret; } public string convertto(string source, string destination) { return source; } }
when call mapper in 1 direction (customer -> customerdao) method 'convertto' called.
since dozer able handle bi-directional mapping, expect that, call mapper in opposite direction, method 'convertfrom' called.
but method convertto never called.
i suspect problem is, both types strings - how can make work?
as workaround created 2 one-way-mapping, standard solution, or behavior bug?
yes, problem source , destination classes same. here dozer source dozerconverter
:
public object convert(object existingdestinationfieldvalue, object sourcefieldvalue, class<?> destinationclass, class<?> sourceclass) { class<?> wrappeddestinationclass = classutils.primitivetowrapper(destinationclass); class<?> wrappedsourceclass = classutils.primitivetowrapper(sourceclass); if (prototypea.equals(wrappeddestinationclass)) { return convertfrom((b) sourcefieldvalue, (a) existingdestinationfieldvalue); } else if (prototypeb.equals(wrappeddestinationclass)) { return convertto((a) sourcefieldvalue, (b) existingdestinationfieldvalue); } else if (prototypea.equals(wrappedsourceclass)) { return convertto((a) sourcefieldvalue, (b) existingdestinationfieldvalue); } else if (prototypeb.equals(wrappedsourceclass)) { return convertfrom((b) sourcefieldvalue, (a) existingdestinationfieldvalue); } else if (prototypea.isassignablefrom(wrappeddestinationclass)) { return convertfrom((b) sourcefieldvalue, (a) existingdestinationfieldvalue); } else if (prototypeb.isassignablefrom(wrappeddestinationclass)) { return convertto((a) sourcefieldvalue, (b) existingdestinationfieldvalue); } else if (prototypea.isassignablefrom(wrappedsourceclass)) { return convertto((a) sourcefieldvalue, (b) existingdestinationfieldvalue); } else if (prototypeb.isassignablefrom(wrappedsourceclass)) { return convertfrom((b) sourcefieldvalue, (a) existingdestinationfieldvalue); } else { throw new mappingexception("destination type (" + wrappeddestinationclass.getname() + ") not accepted custom converter (" + this.getclass().getname() + ")!"); } }
instead of using convertfrom
, convertto
methods (which part of new api), original way in have implement customconverter.convert
shown in tutorial.
Comments
Post a Comment