I am using Datanucleus/JDO to persist objects in a MongoDB DB. I try to persist an object containing a Map, which value type is an abstract class.
When I try to persist an instance of that object, fields of the abstract class are persisted, but not those of the child class.
Below is some code as an example.
Zoo.java
@PersistenceCapable
public class Zoo {
@Persistent
private String fieldZoo;
@Persistent
private Map<String, Animal> mapStringAnimal;
// etc... basic constructor...
}
Animal.java
@PersistenceCapable(embeddedOnly = "true")
public abstract class Animal {
@Persistent
private String fieldAnimal;
}
Dog.java
@PersistenceCapable(embeddedOnly = "true")
public class Dog extends Animal {
@Persistent
private String fieldDog;
}
Test.java
public static void main(String[] args) {
Map<String, Animal> mapStringAnimal = new HashMap<String, Animal>();
Dog dog = new Dog("valueFieldAnimal", "valueFieldDog");
mapStringAnimal.put("dogKey", dog);
Zoo zoo = new Zoo("valueFieldZoo", mapStringAnimal);
Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionURL", "mongodb:/dbtest");
properties.setProperty("javax.jdo.option.Mapping", "mongodb");
properties.setProperty("datanucleus.autoCreateSchema", "true");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
PersistenceManager pm = pmf.getPersistenceManager();
pm.makePersistent(zoo);
pm.close();
}
And when I look at MongoDB:
> db.Zoo.find().pretty();
{
"_id" : ObjectId("50d2f5f7e4b0cae285990b2d"),
"fieldZoo" : "valueFieldZoo",
"mapStringAnimal" : [
{
"key" : "dogKey",
"value" : {
"fieldAnimal" : "valueFieldAnimal"
}
}
]
}
Yes, but DataNucleus doesn't support embedded inherited Map keys/values. It does support embedded inherited Collection elements (part of not-yet-released JDO3.1), but not the equivalent for Maps. Obviously the code is open source and anyone could dive in and contribute it (once you've added a discriminator to "Animal" of course).