I am trying to write and read a complex object to a MongoDB using Java Mongo Driver.
The object includes among other things Lists of Objects of different but related type that I cover with abstract classes in the data model, for example:
public class Datatypes{
public List<DatatypeDefinitionTag> datatypeDefinitions;
}
…which gets filled with these objects:
public class DatatypeDefinitionInteger extends DatatypeDefinitionTag {
[...]
}
public class DatatypeDefinitionString extends DatatypeDefinitionTag {
[...]
}
Now, according to the documentation (POJOs) this should work by annotating the abstract class as follows:
@BsonDiscriminator
public abstract class DatatypeDefinitionTag {
[...]
}
…however, if I do that, I still get an error while trying to read data from the MongoDB, and checking the MongoDB directly, I can see that all entries under datatypeDefinitions
have _t:reqIF.reqIF_dataStructure.abstracts.tags.DatatypeDefinitionTag
I would have expected the discriminator to assign the following values instead, based on the saved object:
_t:reqIF.reqIF_dataStructure.coreContent.reqIF_content.datatypes.datatypeDefinitions.DatatypeDefinitionString
_t:reqIF.reqIF_dataStructure.coreContent.reqIF_content.datatypes.datatypeDefinitions.DatatypeDefinitionInteger
Any idea why this is happening? In essence, I am just looking for the Java equivalent of how this would work with the .Net Driver:
[BsonKnownTypes(typeof(DatatypeDefinitionString), typeof(DatatypeDefinitionInteger))]
public class DatatypeDefinitionTag