Datanucleus JDO Mongodb - Child of Abstract Map Value Not Saved

I am using Datanucleus / JDO to store objects in a MongoDB database. I am trying to save an object containing a map whose value type is an abstract class.

When I try to save an instance of this object, the fields of the abstract class are saved, but not the child classes.

The following is sample code.

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" } } ] } 
+4
source share
1 answer

Yes, but DataNucleus does not support built-in inherited keys / values. It supports built-in inherited collection elements (part of the not yet released JDO3.1), but not the equivalent for maps. Obviously, the code is open source, and anyone can dive into it and contribute (as soon as you add the discriminator to "Animal", of course).

0
source

All Articles