Dozer is attempting to apply a mapping for a destination subclass when processing an instance of it's parent class as the destination.
Take the class heirarchy: Child -> Parent ( Child extends Parent). Class child has a property (childProp) that is not present in the Parent class.
The mapping file contains a one-way mapping from class SourceData to class Child.
Calling dozerMapper.map(sourceData, destination) results in a MappingException:
net.sf.dozer.util.mapping.MappingException: Unable to determine write method for field: childProp class: class dozer.test.Parent
Some debugging has turned up that Dozer is trying to apply the SourceData -> Child mapping when mapping to an instance of Parent. It does this because ClassMapFinder.findClassMap() does not find a direct mapping so it calls findInterfaceOrAbstractMapping() to search for one in the list of custom mappings. The logic in findInterfaceOrAbstractMapping() checks that the class of the destination object (Parent) is assignable from the mapping's destination class (Child), which it is so it selects this custom mapping (SourceData -> Child), which is clearly not going to work.
I found a workaround by declaring an empty mapping for SourceData -> Parent, but this doesn't seem like the right thing to do. Is there another solution?
I'm attaching some source code with a unit test to illustrate the problem.
Log in to post a comment.