There are two issues which should help the situation but aren't there yet.
http://code.google.com/p/morphia/issues/detail?id=82
http://code.google.com/p/morphia/issues/detail?id=22
There is a way to do this but it isn't very modular or clean.
There is an ObjectFactory interface that you can implement (extending
DefaultCreator).
class CustomCreator extends DefaultCreator {
@Override
public Object createInstance(Mapper mapr, MappedField mf, DBObject dbObj) {
if(ReflectionUtils.implementsInterface(mf.getType(),
Address.class) || ReflectionUtils.implementsInterface(mf.getSubType(),
Address.class))
//create and return correct instance of Address
else
return super.createInstance(....);
}
}
Then you must set this to be the ObjectFactory for your instances of
the mapper (in Morphia or the Datastore).
This is all untested and I've written it in textfield in my web-based
mail reader, so please do fix my typo/syntax errors.
I would suggest that for now you not do this because it may change
over time as it is mostly internal bits, but if you must get this
working there are few options. When the other issues above are
resolved there will be a much cleaner way to do this.
If I get time later, and you don't get it working, I can mock up a
quick test to see if there are any (conceptual) issues.